I have a code that calls $.ajax like this :
我有一个代码调用$ .ajax像这样:
$.ajax({
type: "POST",
url: "/sandbox/graphloader/mock3",
async: false,
data: {calInput1:dates[0], calInput2:dates[1]},
success: function(data){
data=eval(data);
for(var x in data[0]){
//alert(data[0][x]);
//fill columns here;
}
fillPercents(column);
}});
now, this works in all browsers, other than Firefox. firebug shows it is getting reply back from post, but for some unknown error, it is not displaying the data. What might be the problem ?
现在,这适用于所有浏览器,而不是Firefox。 firebug显示它正在收到来自帖子的回复,但是对于某些未知错误,它没有显示数据。可能是什么问题?
3 个解决方案
#1
10
This behavior is by design.
此行为是设计使然。
Never use async: false
.
Since Javascript runs on the UI thread, an async: false
request will freeze the browser until the server replies.
永远不要使用async:false。由于Javascript在UI线程上运行,因此async:false请求将冻结浏览器,直到服务器回复。
#2
1
It's freezing because it's not getting a 'success' response and you haven't handled the error. Set an error: function(jqXHR, textStatus, errorThrown){}
in you settings to handle the error response. async: false
works fine.
它是冻结的,因为它没有得到“成功”响应,你没有处理错误。在您的设置中设置错误:function(jqXHR,textStatus,errorThrown){}以处理错误响应。 async:false工作正常。
#3
0
Your columns
global might be getting overwritten by a previous AJAX request.
您的全局列可能会被先前的AJAX请求覆盖。
Try making it a local variable in the callback, and try adding console.log
calls to ensure that the callback order is what you expect.
尝试将其作为回调中的局部变量,并尝试添加console.log调用以确保回调顺序符合您的预期。
You may need to keep a global request counter to ensure that you don't process a response after sending a new request.
您可能需要保留全局请求计数器,以确保在发送新请求后不处理响应。
For example:
var lastRequest = 0; //You may want to put this in a namespace or closure
...
var thisRequest = ++lastRequest;
$.ajax({
type: "POST",
url: "/sandbox/graphloader/mock3",
async: false,
data: {calInput1:dates[0], calInput2:dates[1]},
success: function(data){
if (thisRequest !== lastRequest) return;
...
}});
Do not do this inside a loop body or you'll share the thisRequest
variable.
Instead, call a function from the loop body.
不要在循环体内执行此操作,或者您将共享thisRequest变量。相反,从循环体调用函数。
#1
10
This behavior is by design.
此行为是设计使然。
Never use async: false
.
Since Javascript runs on the UI thread, an async: false
request will freeze the browser until the server replies.
永远不要使用async:false。由于Javascript在UI线程上运行,因此async:false请求将冻结浏览器,直到服务器回复。
#2
1
It's freezing because it's not getting a 'success' response and you haven't handled the error. Set an error: function(jqXHR, textStatus, errorThrown){}
in you settings to handle the error response. async: false
works fine.
它是冻结的,因为它没有得到“成功”响应,你没有处理错误。在您的设置中设置错误:function(jqXHR,textStatus,errorThrown){}以处理错误响应。 async:false工作正常。
#3
0
Your columns
global might be getting overwritten by a previous AJAX request.
您的全局列可能会被先前的AJAX请求覆盖。
Try making it a local variable in the callback, and try adding console.log
calls to ensure that the callback order is what you expect.
尝试将其作为回调中的局部变量,并尝试添加console.log调用以确保回调顺序符合您的预期。
You may need to keep a global request counter to ensure that you don't process a response after sending a new request.
您可能需要保留全局请求计数器,以确保在发送新请求后不处理响应。
For example:
var lastRequest = 0; //You may want to put this in a namespace or closure
...
var thisRequest = ++lastRequest;
$.ajax({
type: "POST",
url: "/sandbox/graphloader/mock3",
async: false,
data: {calInput1:dates[0], calInput2:dates[1]},
success: function(data){
if (thisRequest !== lastRequest) return;
...
}});
Do not do this inside a loop body or you'll share the thisRequest
variable.
Instead, call a function from the loop body.
不要在循环体内执行此操作,或者您将共享thisRequest变量。相反,从循环体调用函数。