1

My jQuery:

jQuery.ajax({
    url: "wahwahweewah",
    type:"post",
    dataType: 'json',
    data: {
        fizz: jQuery('#fizz').val(),
        buzz: jQuery('#buzz').val(),
        foo: "blah"
    },
    success: function(result) {
        alert("Success!");
    },
    error: function(xhr){
        alert("Error!");
    }
});

When I run this in both IE and Chrome, I get the alert defined inside my error handler:

Error!

In both those browsers' developer tools, I inspect the network request and see the following:

Response Headers
================
Response            HTTP/1.1 200 OK
Server              Apache-Coyote/1.1
Content-Type        text/html;charset=utf-8
Transfer-Encoding   chunked
Date                Fri, 31 Oct 2014 15:41:37 GMT

Response Body
=============
Wah wah wee wah

Note that "Wah wah wee wah" is an appropriate response from my server (at this point).

So I ask: Why is my error handler executing?!? According to both browsers I'm getting a valid response from the server with a status of HTTP 200 OK. Ideas?

smeeb
  • 27,777
  • 57
  • 250
  • 447
  • Thanks @apsillers (+1) - Please confirm: if the server doesn't send back application/json, jQuery will interpret that as an error and call the error handler? – smeeb Oct 31 '14 at 15:52
  • 1
    When you tell jQuery to expect JSON, it tries to parse the response. Failure to parse the response will result in the error method being called. This is not about your response's MIME type at this point--simply that you told jQuery you'd get back JSON and then you didn't. – JAAulde Oct 31 '14 at 15:54
  • That was it @apsillers - if you put your comment into the form of an answer, I'll **happily** award you the green check. – smeeb Oct 31 '14 at 15:57

0 Answers0