jQuery $.when 总是失败:什么是 $.when 寻找确定成功/失败?
我需要链接 2 个帖子的结果,并从位于以下位置的延迟示例开始: http://api. jquery.com/jQuery.when/
var successFunction = function (event) { alert(event.readyState); };
var failedFunction = function (event) { alert(event.readyState); };
$.when($.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
.then(successFunction , failedFunction );
在我的例子中,即使事件对象和 chrome 报告以下属性,failedFunction 始终会触发:
readyState: 4
responseText: "OK"
status: 200
statusText: "OK"
使用以下形式时的结果相同:
$.when( $.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
.then(successFunction)
.fail(failFunction);
什么是 $.when 寻找以确定成功/失败?当readyState === 4且status === 200时,如何获取 $.when 来触发 successFunction ?它是否正在寻找我没有从服务器发送的其他内容?
I need to chain a reults of 2 posts and started with the deferred sample found at: http://api.jquery.com/jQuery.when/
var successFunction = function (event) { alert(event.readyState); };
var failedFunction = function (event) { alert(event.readyState); };
$.when($.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
.then(successFunction , failedFunction );
In my case, the failedFunction always fires even though the event object and chrome report the following properties:
readyState: 4
responseText: "OK"
status: 200
statusText: "OK"
same results when using the following form:
$.when( $.ajax("/page1.php", type: 'POST'), $.ajax("/page2.php", type: 'POST'))
.then(successFunction)
.fail(failFunction);
what is $.when looking for to determine success/failure? How do I get $.when to fire successFunction when the readyState === 4 and status === 200? is it looking for something else I'm not sending from the server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 在检查 ajax 请求是否成功时会查找以下状态代码,
除此之外的任何状态代码都将被视为不成功。
由于您有两个 ajax 请求,我怀疑其中一个请求失败。
尝试记录响应并查看哪一个响应失败。
jQuery looks for the following status codes when checking if the ajax request was successful
Anything other than that would be considered not successful.
Since you have two ajax requests i'm suspecting one of them is failing.
Try to log the responses and see which particular one is failing.
感谢您的帮助。我向每个 .ajax 调用添加了一个 error: function(xhr, ajaxOptions, throwError) ,这给了我更多详细信息。两者都返回相同的错误“Unexpected token O”,因为我返回“OK”。一旦我删除退货,问题就消失了。没有机会尝试已完成的技术,但我相信这在将来会有帮助。
Thanks for the help. I added an error: function(xhr, ajaxOptions, thrownError) to each of the .ajax calls which gave me more details. Both were returning the same error "Unexpected token O" because I was returning "OK." Once I removed the return the problem went away. Didn't get a chance to try the done technique but I'm sure that will be helpful in the future.