当我进行 JQuery 调用时,出现“身份验证失败”的响应:
网络方法(Web Method):
{
Message: "Authentication failed.",
StackTrace: null,
ExceptionType: "System.InvalidOperationException"
}
jQuery 调用:
$(document).ready(function () {
//Handle the change event for the drop down list
$("#ddRegions").change(function () {
//create the ajax request
$.ajax({
type: "POST", //HTTP method
url: '<%= ResolveUrl("WebForm2.aspx/getLocations")%>', //page/method name
data: "{}", //json to represent argument
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { //handle the callback to handle response
//request was successful. so Retrieve the values in the response.
alert(msg.toSource());
}
});
});
});
网络方法(Web Method):
public static string getLocations() {
System.Diagnostics.Debug.WriteLine("Getting Locations.");
return "{region:auckland, city:auckland}";
}
我在我的web.config文件中添加了以下内容:
<authorization>
<allow users="*" />
</authorization>
<authentication mode="None" />
我尝试在RouteConfig.cs
中将AutoRedirectMode
设置为Off
。这样可以停止错误,但是Web方法仍然不会被调用。有任何建议吗?
dataType: 'json'
强制 jQuery 自动解析响应数据。但是getLocations()
方法返回无效的 JSON。它应该是:{"region":"auckland", "city":"auckland"}
。双引号对于JSON.parse()
非常重要。 - marcel