10

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

Kivylius
  • 6,357
  • 11
  • 44
  • 71

1 Answers1

13

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" })
nullability
  • 10,545
  • 3
  • 45
  • 63
  • testing.js is just a regular javascript file. I replace the content to just `test` and it's still not working. – Kivylius May 15 '13 at 16:59
  • just `test` is not valid javascript. If you want to retrieve it without parsing it as javascript, set the dataType to `text`. I've updated my answer to reflect this. – nullability May 15 '13 at 17:08