views:

835

answers:

2

Hi all,

I'm making a jQuery getJSON request to another domain, so am making sure that my GET URI ends with "callback=?" (i.e. using JSONP).

The NET panel shows that I am receiving the data as expected, but for some reason the Console logs the following error: "invalid label".

The JSON validates with JSONLint so I doubt that there is anything truly wrong with the structure of the data.

Any ideas why I might be receiving this error?

+1  A: 

It looks like you're misusing JSONP in your server script.

When you receive a request with a callback parameter, you should render the following:

callbackName({ "myName": "myValue"});

Where callbackName is the value of the callback parameter.

SLaks
The jQuery spec allows for "callback=?" when using jQuery.getJSON. This wraps the response in a callback function.
jerome
@jerome: Yes, but your server-side script must support it. JSONP is not magic.
SLaks
@SLaks, thanks for your insight on this. If you have a moment, see the links below for my test case. I don't have enough information yet to know why the cross domain request is not allowing me to use the data.http://www.thespacebetweenthewords.org/json_test/test_jsonp_request.htmlRequest is made to another domain. "invalid label" console error in Firebug, and unable to use the data.http://www.globaloperative.info/json_test/test_json_request.htmlRequest is made to the same domain. Data is retrieved and evaluated correctly.
jerome
You need server-side code to emit the function call. JSONP does not magically retrieve data from a different domain; it needs a compatible server-side script to emit a function call based on the `callback` parameter. See [here](http://niryariv.wordpress.com/2009/05/05/jsonp-quickly/).
SLaks
A: 

This is an old post, but I'm posting a response anyway:

Let's assume you want to get the jSON code generated by the following file, "get_json_code.php":

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>

Like you mentioned, $.getJSON() uses JSONP when you add a "jsoncallback=?" parameter to the required URL's string. For example:

$.getJSON("http://mysite.com/get_json_code.php?jsoncallback=?", function(data){ 
   alert(data);
});

However, in this case, you will get an "invalid label" message in Firebug because the "get_json_code.php" file doesn't provide a valid reference variable to hold the returned jSON string. To solve this, you need add the following code to the "get_json_code.php" file:

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo $_GET['jsoncallback'].'('.json_encode($arr).')'; //assign resulting code to $_GET['jsoncallback].
?> 

This way, the resulting JSON code will be added to the 'jsoncallback' GET variable.

In conclusion, the "jsoncallback=?" parameter in the $.getJSON() URL does two things: 1) it sets the function to use JSONP instead of JSON and 2) specifies the variable that will hold the JSON code retrieved from the "get_json_code.php" file. You only need to make sure they have the SAME NAME.

Hope that helps,

Vq.

vq20