Ajax, script, and 404: behavior changed after jquery upgrade
Hello,
I am trying to upgrade my app from JQuery 2.2.4 to 3.2.1, and I get an error that I did not have before. I pared it down to the following simple example:
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
- <script>
- $.ajax({
- url: 'doesnotexist.js',
- async: false,
- dataType: 'script',
- timeout: 500
- });
- </script>
- </head>
- <body></body>
- </html>
In the Firefox console, I can see the 404-error on the GET request for the (non-existing) javascript file, followed by the error:
- SyntaxError: expected expression, got '<' [Learn More]
What I think is happening, is that JQuery takes the error-document returned from the webserver, and tries to execute it as if it was a valid javascript file. The error-document of course contains html tags that start with '<'.
I would expect JQuery to not interpret an error-document (anything other than the response to a 200 result code) as a javascript file.
When I change the url-property to a javascript resource that does exist, the ajax call functions as expected; it executes that script that was fetched. Also, when I change the JQuery version to 2.2.4, the syntax error in the console disappears.
How can I prevent JQuery from trying to execute error-documents as javascript?
Thanks!