tags:

views:

44

answers:

4
var resturl = "http://example.com";
cj.getJSON( resturl + "&callback=?",        
    function(data){
          console.log( data );
    }
);

My callback function is never called. Any idea??

+4  A: 

Two things here:

First your URL additon should be "?callback=?" since there's no other querystring, or use the full $.ajax() call with a jsonp data type so it adds the querystring as needed, like this:

$.ajax({
  url: resturl,
  dataType: 'jsonp',
  success: function(data){
    console.log( data );
  }
});

Second, the domain you're going to has to support JSONP.

Nick Craver
Dude you rock !!! this worked :)
Kurund Jalmi
@Kurund - good to hear, did you have other issues?
Nick Craver
just curious my request is not shown in firebug..
Kurund Jalmi
@Kurund - It won't show under XHR, it'll show as a normal script request since it's JSONP.
Nick Craver
A: 

This should work as long as the server side sends the data and uses JSONP

var resturl = "http://example.com";
$.getJSON(resturl,{"callback":"?"},function(data, textStatus){ console.log(data)});
Adam
this didn't work :(, trying other..
Kurund Jalmi
A: 

According to the documentation the callback is only executed on success. So the request in general might have failed. The URL that is constructed also doesn't really contain a valid query string. You start the query string with a ? but your url just contains a & which indicates additional query string parameters:

callback(data, textStatus)A callback function that is executed if the request succeeds.

http://api.jquery.com/jQuery.getJSON/

But I'm not sure if the additional JSONP option changes something about this policy.

Horst Gutmann
A: 

Remember that jquery is going to execute your callback as a function, so you've got to make sure that the JSON returned by your server is wrapped as a callback.

Here's a very simple working example.

Javascript:

var myURL = "http://www.someserver.com/somegreatapplication.php?callback=?";
$.getJSON( menuURL, function(data) { 
    $("#somediv").html( data.htmlCode);
});

somegreatapplication.php

<?php

    $output['htmlCode'] = "<b>This worked!</b>";

    // Properly format the jsonP response //
    $json = json_encode( $output);
    header("Content-type: application/json");  
    exit( $_GET['callback'] . ' (' . $json . ');' );
?>

Notice that the PHP will not return Raw JSON code like you're probably used to, but will actually return the callback function, including the JSON. jQuery will turn your URL into something like this:

http://www.someserver.com/somegreatapplication.php?callback=jsonp1283446768493&amp;_=1283446768526

Your PHP would then output this:

jsonp1283446768493({"menuHTML":"<b>This worked!</b>"});

That gets run, and then the object is available within your getJSON( ... function(){} );

All of this of course assumes that you're controlling both ends of the communication. If you don't, then you need to make sure that the other end is giving you proper JSONP output (you may need to use a specifically named callback if they are forcing that)

Coach John