jQuery getScript() sometimes works fine, sometimes returns "parsererror" and TypeError Exception

StackOverflow https://stackoverflow.com/questions/16561904

  •  29-05-2022
  •  | 
  •  

Question

I have strange problem with jQuery (currently 1.9.1 version), I am using getScript() function to load some additional JS library on demand. It loads everytime the same library from the same libraries, so there is no difference between multiple requests. It is always returned from server as "application/javascript". I use only one browser for these requests.

$.getScript(scripts[s], function(){
  onScriptLoaded();
}).done(function(script, textStatus) {
  console.log(textStatus);
}).fail(function(jqxhr, settings, exception) {
  console.log(jqxhr, settings, exception);
});

It usually runs fine, .done() is called and textStatus is success. However, about 1 in 10 requests, it fails and .fail() is called - settings contains parsererror and exceoption contains TypeError {}.

I really have no idea why is this happening, because 9 times out of 10 it works fine. But why it sometimes fails, I really don't know. Can anybody shed some light on this problem ?

I will be glad for any hints or help.

Was it helpful?

Solution

OK, I have found a solution, but I am not happy with it, because I don't know the real cause of this problem.

I used solution mentioned in this answer: https://stackoverflow.com/a/2068597/385264

It's not really an answer, because I have no idea, what and why is going on. I am glad that I got it to work, but I would like if someone could explain it.

I hope, at least, it will help someone with similar problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top