Inconsistent jsonp error: object is not a function

I am trying to use jsonp with jQuery, but I am getting inconsistent behavior. Sometimes a script works, sometimes it is not; I really don't understand why.

This is a bug that might appear in Chrome:

Uncaught TypeError: Property 'jQuery18208278296771459281_1362854738133' of object [object Object] is not a function 

In the following example, I am trying to check if the application is turned on or not. But this inconsistent behavior can occur with other similar ajax calls:

  $.ajaxSetup({ error: function (req, status, ex) {}, success: function (data, status, req) {}, timeout: 2000, crossDomain: true, contentType: "application/json", dataType:"jsonp", url: "http://myUrl.com/ping.php?preventCache="+new Date() }); return $.ajax(); 

The server side PHP file is also quite simple:

 <?php header("Content-Type: application/javascript; charset=UTF-8"); echo $_GET['callback']; ?> ({ "status": "online" }) 
+7
source share
3 answers

your question is not so clear, I got the same error when I tried to use the same callback function name twice. In any case, this is how jsonp should be called in jquery:

  var mycallback = 'c'+Math.floor((Math.random()*100000000)+1); $.ajax({ type: 'GET', url: URL, async: true, jsonpCallback: mycallback, contentType: "application/json", dataType: 'jsonp' }).done(function(json){ console.log(json); callback(json); }).fail(function(f){ console.log("f"); console.log(f.status); }).always(function(){ // console.log('complete'); }); 
+5
source

You will get this behavior when the JSONP timeout expires (this is two seconds, according to your timeout: 2000 string), but the data arrives after the timeout expires.

JSONP does not use regular XmlHTTPRequest. To execute JSONP, jQuery inserts a script tag into your document and creates a temporary function to process the script tag (the function name is a long random string in the error message).

When your timeout occurs and the script has not finished loading, jQuery discards the temporary function. After that, the script tag finishes loading and tries to call this function - but too late the function was deleted. Hence the error.

You may be able to get fewer errors by increasing the latency, but the main problem is that you cannot cancel the loading of the script tag. If he was not mistaken, there is always a chance that it will take a little longer than your timeout. As far as I know, there is no neat solution for this. You can tell jQuery to use the explicitly named function as a JSONP callback instead of creating your own, but then you will need to keep track of whether this thing was a timeout on its own. You can use CORS, which is another whole.

It is probably best to live with him.

+15
source

Check if your script executes the same AJAX request more than once in a row.

I had an erroneous event handler that ran two requests at the same time, and this caused the same problem.

(I assume you call $ .ajaxSetup once): it looks like you intend to apply a cache buffer to your ping calls, but doing this in $ .ajaxSetup may have the opposite effect, because the URL will be fixed for all subsequent requests .

+2
source

All Articles