最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "invalid label" Firebug error with jQuery getJSON - Stack Overflow

programmeradmin2浏览0评论

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 of Firebug shows that I am receiving the data as expected, but for some reason the Console panel 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?

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 of Firebug shows that I am receiving the data as expected, but for some reason the Console panel 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?

Share Improve this question edited Aug 9, 2013 at 13:46 Broxzier 2,94919 silver badges37 bronze badges asked May 12, 2010 at 20:42 jeromejerome 4,98713 gold badges55 silver badges75 bronze badges 1
  • Please show us the Javascript rendered by the JSONP request. – SLaks Commented May 12, 2010 at 20:45
Add a ment  | 

2 Answers 2

Reset to default 11

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./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.

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.

发布评论

评论列表(0)

  1. 暂无评论