0

I have a script on localhost that sends a GET request to the same domain. It results in 304 response, which apparently JQuery treats as an error.

$(document).ready(function(){
    $.ajax({
        type: 'GET',
        url: 'http://localhost/file.js',
        error: function(e) {
            console.log('error: ' + e.responseText); // I see this message in console
        },
        success: function(e) {
            console.log('success: ' + e.responseText); // I don't see this message in console
        }
    });
});

1) Why do I get a 304 response? 2) How can I modify the code so that the success function gets called? (instead of error function)

  • My guess is the error stems from you receiving a .js file, jQuery is expecting json – labago Apr 11 '16 at 20:49
  • No problem, added it – labago Apr 11 '16 at 20:53
  • 1
    for future reference, 304 is not an error but a server response saying the file hasn't changed it is cached. jQuery SHOULD handle this correctly, but it might not be. Another solution is to add `cache: false` – imvain2 Apr 11 '16 at 21:08

2 Answers2

1

My guess is the error stems from you receiving a .js file, jQuery is expecting json

labago
  • 1,338
  • 2
  • 12
  • 28
0

if you are loading a js file, add dataType: "script" to your ajax to force it expect js

imvain2
  • 15,480
  • 1
  • 16
  • 21