why ajax error is different in IE and Chrome?

942 views Asked by At

Lets take example of the following code below.

<script>
    function abc()
    {
        $.ajax({
            type: "GET",
            url:    "https://zx/abc/def",
            timeout: 6000,
            dataType: "jsonp",
            error: function(h, j, e) {
              console.log(h+"  "+j+"  "+e);
            }
        })
    }
</script>

<button onclick="abc()">Start Call</button>

The url used above is a non-existant/invalid one. Now lets see whats the output of the above code in different browsers:

Chrome,firefox, IE 11 ->   [object Object]  timeout  timeout

IE 8,9,10 ->               [object Object]  parsererror  Error: jQuery111308894510177821542_1433915740650 was not called

So the question why we get different error messages?

1

There are 1 answers

1
Henrik On BEST ANSWER

Where do we start on this.

Do we target the answer to first time ajax programmers, or to very experienced programmers? - and what is actually being asked here?

The simple answer is obvious. The browsers are different, so they react to an error in different ways, which propagate to jquery, and finally results in two different error messages. Since jquery wraps the very browser depending javascript engines, it will not always be able to behave in a fully crossbrowser way.

However thats really not what you are asking now is it? You probably want to know what's going wrong..

One browser states that you have a timeout, the other that the browser is unable to parse the recieved data, and fails to call a jquery function, that is reponsible of doing so...

you may want to read this related question: Callback - Parseerror JSONP via jQuery AJAX

The most probable cause IMHO is that the the data you are recieving from the server is not actually of the type jsonp.. or it is incomplete, or broken. It may be text? or xml? or just plain and simple json.

try changing the type to text, and see if the error messages change.

  • but... why does chrome not give the same error as IE?

well, if I'm right about the data not actually being proper jsonp, it could be related to what happens when jquery "parses" json. Jquery will use the browser built in methods (when available), a probable reason is that chrome is used to parse the data, but when something goes wrong, it times out

  • where can I get documentation?

So many places have bits and pieces of the documentation required to understand this. To explain this genericly for all kinds of data, and possible errors, refer to the Jquery source code, and the javascript engine documentation for I.E and Chrome respectively.

It can take a long time to read, since the behavhior is different for different versions of I.E and Chrome. In the old days we had to learn this just to make the simplest crossbrowser things work. Jquery hides most of the differences, and this is my best bet on the succes of jquery. We no longer need to know all those subtle differences. We just need to know where to look when something goes wrong..

A good place to start is here: http://api.jquery.com/jquery.parsejson/

Where the browser provides a native implementation of JSON.parse,