Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax fails but status code is still 200 "OK"

Tags:

jquery

ajax

I'm not sure why this is happening but i have a simple Ajax Code:

$.ajax({ url: "/javascript/testing.js"})
    .done(function(data){ console.log(data) })
    .fail(function(jqXHR, textStatus, errorThrown) {
         console.log(jqXHR);
    });

.fail() get's executed the status code is "OK". Also the data is present in responceText to the actual legit data. Why is this happening?

enter image description here

like image 416
Kivylius Avatar asked May 15 '13 16:05

Kivylius


People also ask

What does AJAX error mean?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.

What is 500 Internal Server Error AJAX?

The 500 Internal Server Error is a very general HTTP status code. It means something has gone wrong on the website and webserver is unable to specify what exactly, thus failing in fulfilling the request made by the client. Reload the web page. Clear your browser's cache.

What does AJAX Error 0 mean?

Status code 0 means the requested url is not reachable.


1 Answers

If you want to parse the javascript file, then the dataType should be script:

$.ajax({ url: "/javascript/testing.js", dataType: "script" })
.done(function(data){ console.log(data) })
.fail(function(jqXHR, textStatus, errorThrown) {
     console.log(jqXHR);
});

If you are still getting a parserError then there is a problem with your testing.js file.

If you don't want to parse it and just retrieve it, then the dataType should be text:

$.ajax({ url: "/javascript/testing.js", dataType: "text" })
like image 192
nullability Avatar answered Oct 03 '22 14:10

nullability