[英]Why JQuery AJAX call is not working?
以下是我的JQuery AJAX方法:
function meFun() {
alert('enter');
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do something interesting here.
alert(msg);
}
});
alert('end');
}
以下是我的按钮代码:
<input type="button" onclick="meFun();" value="Click me" id="btn" />
滚动是About.aspx中的功能
Public Function GetRes() As Boolean
Return True
End Function
meFun()
方法已成功调用, GetRes()
调用GetRes()
,并且AJAX调用未返回任何响应。
知道为什么吗? 另外,请提出一种调试此类AJAX问题的好方法。
首先,确保服务器端具有GetRes函数的WebMethod属性(这在C#中,我不知道您如何在VB中做到这一点)并使GetRes静态(或VB中的任何等效功能)。
[WebMethod]
public static bool GetRes()
{
return true;
}
然后,将error
处理程序添加到您的jQuery ajax
调用中:
$.ajax({
type: "POST",
url: "About.aspx/GetRes",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do something interesting here.
alert(msg);
},
error: function (data) {
}
});
alert('end');
编辑:调试,在错误处理程序中设置一个断点,并检查data
参数。 所有浏览器(我可以担保IE,Firefox(带有Firebug)和Chrome)都具有良好的脚本调试器。 如果发生错误,则data
中将存在一个属性,此属性目前尚不明确,该属性详细说明了该错误。
当您的AJAX调用预期在服务器端使用JSON格式的AJAX调用时,您将在STRING格式中返回数据通过使用newtonsoft库或asp.net的内置jsonconversion机制将TRUE转换为JSON格式
如svajger所提到的,您必须将其声明为webmethod,并且必须为静态,返回类型为字符串型
在成功处理程序中,您必须通过JSON.parse方法解析json数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.