请温柔,因为我还是新的网页编程和-very-新的Ajax!
Please be gentle, as I'm still new to web programming and -very- new to Ajax!
我已经创建了一个C#函数,它从一个MSSQL数据库提取数据,其格式为JSON字符串,并返回。现在我需要通过相关的C#code文件一个aspx页面,以从我的javascript调用(jQuery的)滑块。
I've created a C# function which extracts data from an mssql database, formats it to a json string and returns that. Now I need to make a call from my javascript (jQuery) slider through an aspx page that is related to the C# code file.
我其实从来没有做过这样的事,从我可以告诉google搜索,我需要使用xmlHtt prequest,但究竟如何让我的功能弄个这串?
I have actually never done anything like this before, from what I could tell by googling I need to use xmlHttpRequest, but how exactly do I make the function get hold of this string?
这将是真棒,如果有人有一些例如code,显示了这一过程。
It would be awesome if someone had some example code that shows how this works.
这是使用jQuery相对比较容易,如果你标记C#函数作为[WebMethod的]或使其部分ASP.NET web服务的。这两种技术可以很容易地有自动转换成ASP.NET JSON对象,这使得处理客户端更容易(恕我直言)的响应。
It's relatively easy with jQuery if you mark the C# function as a [WebMethod] or make it part of a ASP.NET webservice. Both these techniques make it easy to have the response automatically converted into a JSON object by ASP.NET, which makes processing on the client easier (IMHO).
下面的例子是,如果页面有一个的WebMethod
的的GetData ,但它是微不足道的更改URL,如果创建服务。
The example below is if the page has a WebMethod
named GetData
, but it's trivial to change the URL if you create a service.
$.ajax({ url: "somepage.aspx/GetData",
method: "POST", // post is safer, but could also be GET
data: {}, // any data (as a JSON object) you want to pass to the method
success: function() { alert('We did it!'); }
});
在服务器:
[WebMethod]
public static object GetData() {
// query the data...
// return as an anonymous object, which ASP.NET converts to JSON
return new { result = ... };
}