开发者

JQuery How to find out what the ajax error is?

I 开发者_如何学JAVAhave the following bit of code which I'm just trying out by running in firebug

$.ajax({
  type:"POST",
  url:"http://mpdomain/WebService.asmx/Operation",
  data: "{'parameter1': '44906'}", 
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  success: function(data) { alert("succsess") },
  error: function(e, ts, et) { alert(ts) }
})

In theory it should work. However the error handler is triggered, and ts is simply set to "error". How do I get more detail about what's gone wrong?


To see the error message from an AJAX request, you can do something like this:

$.ajax({
  type:"POST",
  url:"http://mpdomain/WebService.asmx/Operation",
  data: "{'parameter1': '44906'}", 
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  success: function(data) { alert("success") },
  error: function(ts) { alert(ts.responseText) } // or console.log(ts.responseText)
});

Note that, inside the error function, you get the responseText.


The error message jQuery gives you is not very descriptive. It can either be "timeout", "error", "notmodified" or "parsererror." http://api.jquery.com/jQuery.ajax/ so what you can conclude is that it's not a timeout, not modified or parse error that you are getting.

Make sure in Firebug you see the request set to the correct address and the correct data is being set. You can also view the response so if you also have access to the server code a quick and dirty way is just to echo what is going on server side and view the response with Firebug.

Also I'm not sure if this is an issue but try to set the data to {parameter1: 44906} (basically remove the quotes so you are passing in an object and not a string).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜