The following is my JQuery AJAX method:
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');
}
The following is my button code:
<input type="button" onclick="meFun();" value="Click me" id="btn" />
Folling is my function in About.aspx
Public Function GetRes() As Boolean
Return True
End Function
The meFun()
method is called successfully but doesn't call the GetRes()
and the AJAX call doesn't return any response.
Any idea why? Also, please suggest a good way to debug these sorts of AJAX problems.
First, make sure you have the WebMethod attribute on your server side GetRes function (this is in C#, I don't know how you do that in VB) and make GetRes static (or whatever the equivalent in VB is).
[WebMethod]
public static bool GetRes()
{
return true;
}
Then, add the error
handlers to your jQuery ajax
call:
$.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');
EDIT: to debug, set a breakpoint in the error handler and inspect the data
parameter. All browsers (well, I can vouch for IE, Firefox (with Firebug) and Chrome) have good script debuggers. If an error occurs, there will be a property in data
, the name of which eludes me at the moment, that explains the error in detail.
you are returning a data in a STRING FORMAT WHILE YOUR AJAX CALL EXPECTING A JSON FORMAT SO AT THE SERVER SIDE CONVERT TRUE TO JSON FORMAT BY using newtonsoft library or inbuilt jsonconversion mechanism of asp.net
as mentioned by svajger you have to declare it as webmethod and it must be static the return type is string prefered
and at the success handler you have to parse json data by JSON.parse method
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.