Search code examples
jqueryajaxjsonp

jsonp inconsistent error: object is not a function


I'm trying to use jsonp with jQuery but I get inconsistent behaviors. Sometimes the script works, sometimes it does not; I don't really understand why.

This is the error that may be displayed by Chrome:

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

In the following example, I'm only trying to check whether the application is online or not. But this inconsistent behavior may happen on 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 pretty simple too:

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

Solution

  • your question is not so clear , I was getting the same error when tried to use the same callback function name twice. in Any case this is how the 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');
          });