JSONP 和无效标签

发布于 2024-07-16 14:32:24 字数 1427 浏览 5 评论 0原文

使用 mootools 和 JsonP 我在 Firefox 中收到“无效标签”错误 错误控制台

JsonP 似乎可以工作(我正确获取数据),

{"jsondata":[{"title":"title1","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/17_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:26:00",},{"title":"title2","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/16_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:08:09",}]}

但我在“jsondata”上收到无效标签错误,

同一文件在


删除 request.json 逗号后效果很好。没什么,

这是我正在使用的代码

window.addEvent('domready', function() {

var gallery = $('gallery'); 

new JsonP('http://myjsoncodeurl',{
        onComplete: function(jsonObj) {
            addImages(jsonObj.jsondata);
        }
    }).request();

var addImages = function(images) {
    images.each(function(image) {
        var el = new Element('div', {'class': 'item'});
        var name = new Element('h3').inject(el);
        var a1 = new Element('a', {'href': image.link,'html': image.title}).inject(name);                       
        var desc = new Element('span', {'html': image.description}).inject(name, 'after');
        var a2 = new Element('a', {'href': image.link}).inject(desc,'after');               
        var img = new Element('img', {'src': image.thumbsrc}).inject(a2);
        el.inject(gallery);
    });
};

});

它适用于正常的 request.Json,但 JSONP 不喜欢我的代码:(

Using mootools and JsonP I get "invalid label" error in Firefox Error console

JsonP seems to work (I get the data correctly)

{"jsondata":[{"title":"title1","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/17_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:26:00",},{"title":"title2","link":"http://xxxx.xxx.xxx","thumbsrc":"http://xxxx.xxx.xxx/16_t.jpg" ,"description":".......","pubDate":"2009-03-09 06:08:09",}]}

but I get the Invalid label error on "jsondata"

the same file works good with request.json


comma removed... nothing

this is the code I'm using

window.addEvent('domready', function() {

var gallery = $('gallery'); 

new JsonP('http://myjsoncodeurl',{
        onComplete: function(jsonObj) {
            addImages(jsonObj.jsondata);
        }
    }).request();

var addImages = function(images) {
    images.each(function(image) {
        var el = new Element('div', {'class': 'item'});
        var name = new Element('h3').inject(el);
        var a1 = new Element('a', {'href': image.link,'html': image.title}).inject(name);                       
        var desc = new Element('span', {'html': image.description}).inject(name, 'after');
        var a2 = new Element('a', {'href': image.link}).inject(desc,'after');               
        var img = new Element('img', {'src': image.thumbsrc}).inject(a2);
        el.inject(gallery);
    });
};

});

it works with normal request.Json, but JSONP that doesn't like my code :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何止钟意 2024-07-23 14:32:24

同一个文件可以很好地使用
请求.json

使用 JSONP,您的响应应该返回 JavaScript 函数调用(即回调),并将 JSON 数据作为参数传入。 如果您的响应是普通的旧 JSON 文本,则它在 JSONP 上下文中不起作用。 您必须定制后端以接受回调参数并使用 JSON 数据调用该回调。

the same file works good with
request.json

With JSONP, your response should be returning a JavaScript function call (i.e. callback) with the JSON data passed in as the argument. If your response is a plain old JSON text, it won't work in the context of JSONP. You have to tailor your backend to accept a callback argument and call that callback with the JSON data.

英雄似剑 2024-07-23 14:32:24

你需要在你的对象周围放置方括号(普通的,而不是大括号),因为有时 Javascript 会变得非常混乱,并认为你正在做一个标签语句,这是一种在我用 Google 搜索这个问题之前我不知道存在的语句类型。

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Statements#label_Statement

尝试将您的对象 {"jsondata":[ ... ]} 传递为 ({"jsondata":[ ... ]}) 。 这似乎可以排序。

You need to put brackets (normal ones, not curly ones) around your object, because sometimes Javascript gets horribly confused and thinks you're doing a label statement, a statement type that I didn't know existed until I Googled this problem.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Statements#label_Statement

Try passing your object, {"jsondata":[ ... ]} , as ({"jsondata":[ ... ]}) instead. That seems to sort it.

冰之心 2024-07-23 14:32:24

将其放在这里:

http://json.parser.online.fr/

显示其有效, 但有额外的逗号(这会使 IE 崩溃,尽管 FF 应该处理它)。 如果删除逗号无法解决问题,您需要发布更多代码来帮助我们找到错误。

Putting it in here:

http://json.parser.online.fr/

Shows that its valid, but has the extra comma (which will bork IE, although FF should handle it). If removing the comma doesn't fix it, you'll need to post more of your code to help us find the error.

〃温暖了心ぐ 2024-07-23 14:32:24

这可能是由于日期后面有多余的逗号

This could be due to the extra commas after the dates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文