1

Consider:

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

My callback function is never called. Why?

2
  • Have you checked if the request succeeds? Commented Sep 2, 2010 at 16:46
  • Test you page with firebug it will show the problem. Commented Sep 2, 2010 at 16:49

4 Answers 4

13

Two things here:

First, your URL addition 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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Kurund - It won't show under XHR, it'll show as a normal script request since it's JSONP.
1

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(myURL, function(data) {
    $("#somediv").html(data.htmlCode);
});

File 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 code will not return raw JSON code like you're probably used to, but it 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&_=1283446768526

Your PHP code 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).

1 Comment

$.getJSON( menuURL, function(data) { should be $.getJSON( myURL, function(data) {
0

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)});

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.