I'm using the MVC.net framework and running into a strange issue with a jQuery ajax call.
I have a page that allows the user to enter an amount and then click ‘next’. When they click ‘next’, I make an ajax call to the server and return some updated information via JSON. This is all pretty standard stuff.
However, the strangeness is with the JSON data that is returned. The 'data' variable (in the callback) is sometimes null and sometimes it contains the actual data from the server. The strange thing is that the controller’s action is always cal开发者_StackOverflowled, but the data returned does not always make it into the ajax call-back function. It doesn’t have anything to do with a timeout interval as setting the value has no effect on the issue.
I can often trigger the condition when i'm debugging the action code but the condition does not happen as frequently when i'm not debugging.
Has anyone run into a similar issue or have suggestions on work-arounds or other things to investigate?
var postbackUrl = "/Balance/Add?amt=" + $("#Amount").val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: postbackUrl,
data: "{}",
dataType: "json",
success: function (data,status,request) {
$("#x_amount").val(data.x_newBalance)
}
});
UPDATE: In an attempt to gain more insight, I've switch the callback to use the 3 variables version. In all cases, the status is 'success'. The interesting thing is with the request.
The request always has a status of 200 and a readyState of 4. The difference is with the responseBody. As you would expect, when 'data' is null, the responseBody is null and when 'data' is valid then the responseBody has content.
I guess I need to figure out why the responseBody is being stripped out.
You are using JSON as request content type and still provide parameters in the query string. Why not simply use:
$.ajax({
type: 'POST',
url: '/Balance/Add',
data: { amt: $('#Amount').val() },
dataType: 'json',
success: function (result) {
$('#x_amount').val(result.x_newBalance);
}
});
Also notice that I've used result
as variable to avoid any confusion with data
.
Have you tried the jquery-ajax "timeout" parameter?
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: postbackUrl,
data: "{}",
timeout: 20000,
dataType: "json",
success: function (data) {
$("#x_amount").val(data.x_newBalance)
}
});
I am seeing the success callback passed to $.ajax called when the remote server is unavailable, in that case the data value is null. This doesn't seem right, maybe its related to what you are seeing.
update My issue was a bug in jQuery 1.4.2, maybe you're hitting the same issue? http://bugs.jquery.com/ticket/6172
精彩评论