"Invalid label" Firebug error with jQuery getJSON

I'm making a jQuery request $.getJSON

to a different domain, so I'm pretty sure my GET URI ends with "callback =?" (i.e. using JSONP).

The NET Firebug pane shows that I am receiving data as expected, but for some reason the console pane logs the following error: "Invalid Label".

JSON validates with JSONLint , so I doubt there is anything really wrong with the data structure.

Any ideas why I might be getting this error?

+2


a source to share


2 answers


This is an old post, but I post the answer anyway:

Suppose you want to get the jSON code generated in the following file: "get_json_code.php":

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

      

As you mentioned, $ .getJSON () uses JSONP when you add "jsoncallback =?" parameter to the required URL string. For instance:

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

      

However, in this case, you will receive an "invalid shortcut" message in Firebug because the "get_json_code.php" file does not provide a valid reference variable to store the returned jSON string. To solve this problem, you need to 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].
?> 

      

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

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

Hope it helps,

Ud.

+11


a source


It looks like you are using JSONP incorrectly in your server script.

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



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

      

Where callbackName

is the value of the callback parameter.

+3


a source







All Articles