究竟是什么触发了jQuery ajax成功? [英] What exactly triggers jQuery ajax success?
问题描述
我正在Perl Web框架中构建一些Ajax.Dancer
我不确定它是否以正确的http头作为响应,因为我无法从看似成功的请求中触发jQuery的ajax成功处理程序.使用下面的ajax代码段,我在浏览器控制台中获得以下输出.完整的回调被成功调用,并给出看起来像成功的输出. Status:200 StatusText:"OK"
但是,成功处理程序永远不会被调用.
I am building some ajax in a Perl web framework Dancer
I am not sure it is responding with proper http headers as I cannot trigger jQuery's ajax success handlers from what appear to be otherwise successful requests. Using the ajax snippet below I get the following output in a browser console. The complete callback gets called successfully and gives what looks like successful output. Status:200 StatusText:"OK"
However the success handlers never get called.
$.ajax({type: "GET", url: "/learn/faq",
success: function(data){console.log('omg got it');},
complete: function(data){console.log("complete", data);}
}).success(function(data){console.log('defered');});
Object
XHR finished loading: "https://www.localhost:4443/learn/faq". assets-d36e1bb9fd59ba3dbd0f8a0cbb37ed8e.js:1
complete
Object {readyState: 4, responseText: "↵↵<!DOCTYPE html>↵<html xmlns="http://www.w3.org/1…ead/conversion.js"></script>↵↵↵↵</body>↵</html>↵↵", status: 200, statusText: "OK"
我应该看到omg got it
和defered
消息,但是没有.从这个角度来看,我觉得jQuery成功处理程序的意义不仅仅在于状态,而且Dancer http实现无法正确响应.
I should be seeing the omg got it
and defered
messages but am not. Looking at this I feel there is more to the jQuery success handler than the status and Dancer http implementation is not responding correctly.
此后,我还向代码段添加了error
处理程序,并且错误处理程序被看似成功的请求触发.
Further more I have since added an error
handler to the snippet and the error handler is getting triggered with what looks like a successful request.
$.ajax({type: "GET", url: "/learn/faq",
success: function(data){console.log('omg got it');},
complete: function(data){console.log("complete", data);},
error: function(data){console.log("error!", data);}
}).success(function(data){console.log('defered');});
Object
XHR finished loading: "https://www.localhost:4443/learn/faq". assets-8cd028b93e0db9dd9455125dc98d5ae1.js:1
error!
Object {readyState: 4, responseText: "↵↵↵↵↵↵↵↵↵↵↵↵↵↵↵↵<!DOCTYPE html>↵<html xmlns="http:…></script>↵↵↵↵</body>↵</html>↵↵↵↵</body>↵</html>↵", status: 200, statusText: "OK"}
complete
Object {readyState: 4, responseText: "↵↵↵↵↵↵↵↵↵↵↵↵↵↵↵↵<!DOCTYPE html>↵<html xmlns="http:…></script>↵↵↵↵</body>↵</html>↵↵↵↵</body>↵</html>↵", status: 200, statusText: "OK"}
这是jQuery getAllResponseHeaders()
complete Date: Tue, 01 Jan 2013 22:43:52 GMT
Content-Encoding: gzip
X-Powered-By: Perl Dancer 1.3095.1
Transfer-Encoding: chunked
Connection: keep-alive
Server: nginx/1.2.4
Strict-Transport-Security: max-age=2592000
Content-Type: text/xml; charset=utf-8
推荐答案
如果
The success
handler will be triggered if
- 响应的HTTP状态代码为
200
(您确实如此) - jQuery能够根据响应中的
Content-Type
标头(或dataType
选项(如果提供)覆盖了Content-Type
标头)反序列化响应.
- The response has HTTP status code
200
(yours does) - jQuery is able to deserialize the response according to the
Content-Type
header in the response (or thedataType
option, if you provide it, which overrides theContent-Type
header)
例如,如果您的响应的Content-Type
标头为application/json
或application/xml
,则您引用的响应将不会触发success
处理程序,因为它无法成功反序列化为JSON或XML.
So for instance, if your response had a Content-Type
header of application/json
or application/xml
, the response you've quoted won't trigger the success
handler because it cannot be successfully deserialized as either JSON or XML.
您最新的编辑内容(截至撰写本文时)揭示了问题所在:
Your latest edit (as of this writing) reveals the problem:
这是jQuery getAllResponseHeaders()的响应标头
Here are the response headers from jQuery getAllResponseHeaders()
...
Content-Type: text/xml; charset=utf-8
我怀疑您的回复不是有效的XML,因此jQuery无法将其反序列化为XML,因此失败了.
I suspect your response isn't valid XML, so jQuery can't deserialize it as XML, so it's failing.
这篇关于究竟是什么触发了jQuery ajax成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!