What exactly triggers the success of jQuery ajax?

I am creating ajax in the Perl Dancer web infrastructure. I am not sure if it responds with the proper HTTP headers, since I cannot run jQuery ajax handlers for success from what appears to be successful. Using the ajax snippet below, I get the following output in the browser console. A full callback is called successfully and gives what looks like a successful output. Status:200 StatusText:"OK" However, success handlers are never 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" 

I should see omg got it and defered , but no. Looking at this, I feel that the jQuery success handler is more than status, and the Dancer http implementation is not responding correctly.

Moreover, since then I have added an error handler to the fragment, and the error handler starts 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"} 

Here are the response headers from 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 
+6
source share
1 answer

The success handler will run if

  • The response has an HTTP status code of 200 (yours)
  • JQuery is capable of converting the response according to the Content-Type header in the response (or the dataType option, if you provide it, which overrides the Content-Type header)

So, for example, if your answer was the Content-Type header application/json or application/xml , the response you quoted did not call the success handler because it could not be successfully deserialized either as JSON or XML.


Your last edit (from the moment of writing) reveals the problem:

Here are the response headers from jQuery getAllResponseHeaders ()

...

Content-Type: text/xml; charset=utf-8

I suspect your answer is invalid XML, so jQuery cannot deserialize it as XML, so it does not work.

+12
source

All Articles