“无效标签”使用jQuery getJSON的Firebug错误 [英] "invalid label" Firebug error with jQuery getJSON

查看:184
本文介绍了“无效标签”使用jQuery getJSON的Firebug错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向另一个域发出jQuery $。getJSON 请求,所以我确保我的GET URI以callback =?结尾(即使用JSONP)。

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

Firebug的NET面板显示我按预期接收数据,但由于某种原因,控制台面板记录以下错误:无效标签。

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

JSON使用 JSONLint 进行验证,所以我怀疑数据的结构有什么问题。

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?

推荐答案

这是一篇旧帖子,但我还是发布了一条回复:

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

让我们假设你想要获取由以下文件生成的jSON代码,get_json_code.php:

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

就像你提到的那样,当你添加jsoncallback =?时,$ .getJSON()会使用JSONP。所需URL字符串的参数。例如:

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

但是,在这种情况下,你会在Firebug中收到无效标签消息,因为get_json_code .php文件没有提供有效的引用变量来保存返回的jSON字符串。要解决此问题,您需要将以下代码添加到get_json_code.php文件中:

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].
?> 

这样,生成的JSON代码将被添加到'jsoncallback'GET变量中。

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

总之,jsoncallback =? $ .getJSON()URL中的参数做了两件事:1)它将函数设置为使用JSONP而不是JSON,2)指定将保存从get_json_code.php文件中检索的JSON代码的变量。你只需要确保他们有相同的名字。

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.

希望有所帮助,

Vq。

这篇关于“无效标签”使用jQuery getJSON的Firebug错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆