我在ASP.NET中创建了一个Webmethod,并试图通过jQuery AJAX访问它。当我通过浏览器调用Webmethod时,它给出了一个JSON结果,但是当我通过jQuery AJAX调用它时,它在火狐的Firebug中给出了"204没有内容“。(我的webmethod在一个域中,jQuery AJAX代码在另一个域中)。
JQuery Ajax代码:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:1177/api/authentication/getusermenu?roleid=1",
processData: true,
dataType: "jsonp",
success: function (msg) {
alert(msg.d);
},
error: function (jqXHR, textStatus, errorThrown) {
var data = $.parseJSON(jqXHR.responseText);
alert(data);
}
});
Firebug输出:
发布于 2014-11-25 04:32:27
尝试如下: 1.如果webmethod在同一个应用程序中,那么就不需要在任何其他机器上给localhost或anything.if web方法,并且它是可访问的,然后提供url:"http:///...“而不是localhost:portNo。2. dataType:"json",
以下是完整的示例:
$.ajax({
type: "POST",
url: "ABC.aspx/GetUserMenu?roleid=1",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (jqXHR, textStatus, errorThrown) {
var data = $.parseJSON(jqXHR.responseText);
alert(data);
}
});
https://stackoverflow.com/questions/21399767
复制