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

查看:29
本文介绍了“无效标签"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:

假设您想获取由以下文件get_json_code.php"生成的 json 代码:

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

就像您提到的,$.getJSON() 在您添加jsoncallback=?"时使用 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天全站免登陆