问题描述
我知道可以将AJAX请求发送到.asmx
页.而且我也知道.asmx
页面通过Web方法处理AJAX请求.
I know that it is possible to send an AJAX request to an .asmx
page. And I also know that an .asmx
page handles an AJAX request via a web method.
是否还可以将AJAX请求发送到.aspx
页?如果是,.aspx
页面是否也通过Web方法处理AJAX请求?请注意,我想从.aspx
页面返回JSON响应.这可能吗?
Is it also possible to send an AJAX request to an .aspx
page? If so, does an .aspx
page also handle an AJAX request via a web method? Note that I would like to return a JSON response from the .aspx
page. Is this possible?
推荐答案
您可以在.aspx
页面的代码背后定义Web方法,然后调用它们:
You can define web methods in the code-behind of your .aspx
page and then call them:
[WebMethod]
public static string doSomething(int id)
{
...
return "hello";
}
然后,在您的jQuery代码中调用Web方法:
And then, to call a web method in your jQuery code:
$.ajax({
type: "POST",
url: "YourPage.aspx/doSomething",
data: "{'id':'1'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
var returnedstring = data.d;
var jsondata = $.parseJSON(data.d);//if you want your data in json
}
});
这里很好链接以开始使用.
Here is a good link to get started.
这篇关于将AJAX请求发送到.aspx页并返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!