使用jQuery获取JSON数据会返回无效的标签错误 [英] Using jQuery to get json data returns invalid label error

查看:73
本文介绍了使用jQuery获取JSON数据会返回无效的标签错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,并且还尝试过使用$ .getJson函数进行类似的操作:

I have this code, and have also tried something similar using the $.getJson function:

jQuery(document).ready(function(){
    var kiva_url = "http://api.kivaws.org/v1/loans/newest.json";

    jQuery.ajax({
        type: "GET",
        url: kiva_url,
            data:"format=json", 
        success: function(data){
            alert("here");
            jQuery.each(data.loans, function(i, loan){
                jQuery("#inner_div").append(loan.name + "<br />");
            });
        },
        dataType: "jsonp",
        error: function(){
            alert("error");
        }
    });

});

当我查看Firebug时,它返回无效标签"错误.我搜索了一些人提到的使用解析器来解析结果的信息.我可以在Firebug中看到结果.有人可以指出我应该做什么的例子吗?

When I look in Firebug it is returning an "invalid label" error. I've searched around a bit some people refer to using a parser to parse the results. I can see the results coming back in Firebug. Can someone point to an example of what I should be doing?

Firebug错误:

The Firebug error:

无效标签 http://api.kivaws.org/v1/loans/newest.json?callback=jsonp1249440194660&_=1249440194924&format=json& 第1行

invalid label http://api.kivaws.org/v1/loans/newest.json?callback=jsonp1249440194660&_=1249440194924&format=json& Line 1

可以在以下位置找到json的示例输出: http://build.kiva.org/docs/data/loans

Sample output of what the json looks like can be found here: http://build.kiva.org/docs/data/loans

推荐答案

好吧,我找到了答案……看来kiva不支持jsonp,这是jquery在这里做的事情-

Well I found the answer...it looks like kiva does not support jsonp which is what jquery is doing here -

http://groups.google.com/group/build- kiva/browse_thread/thread/9e9f9d5df821ff8c

...我们没有计划支持JSONP. 支持这个提倡穷人 安全做法,还有 已经有一些很好的方法来访问 来自JavaScript的数据可保护您的 应用程序和您的用户.这是一个 关于该主题的精彩文章:

...we don't have plans to support JSONP. Supporting this advocates poor security practices and there are already some good ways to access the data from JavaScript that protect your application and your users. Here's a great article on the subject:

http://yuiblog.com/blog/2007 /04/10/json-and-browser-security/

尽管Kiva放款人的风险较低 现在,因为我们只处理 公共数据,允许私人贷方 通过脚本标签导入的数据是 未来的风险.我们的 以为是风险(和复杂性 已添加以创建安全的应用程序) 不值得受益 开发人员.

While the risk to Kiva lenders is low now since we are only dealing with public data, allowing private lender data to be imported via script tags is a risk further down the road. Our thought is the risk (and complexity added to create secure applications) is not worth the benefit to developers.

为该服务器编写服务器端代理 您需要的饲料是最常见的 解决方案中的数据访问 基于浏览器的应用程序.一些 使用iFrame还有其他技巧.这 最好的希望是新的客户- 基于技术/标准的 允许基于浏览器的JavaScript访问 安全地跨域资源( http://dev.w3.org/2006/waf/access-control/ http://json.org/JSONRequest.html ). 一些工具,例如Br​​owserPlus和Gears 今天让你玩这些,但是你 将无法依靠这些 野蛮一会儿.

Writing a server-side proxy for the feeds you need is the most common solution to accessing data in browser-based applications. Some other tricks exist using iFrames. The best hope is the new breed of client- based technologies/standards that will let browser-based JavaScript access cross-domain resources securely ( http://dev.w3.org/2006/waf/access-control/ http://json.org/JSONRequest.html ). Some tools like BrowserPlus and Gears let you play with these today, but you won't be able to depend on these in the wild for a while.

作为最后一点,我要指出 任何使用JSON响应的人 JavaScript应该解析JSON 显式或验证JSON之前 接受eval().看到这里:

As a final note, I'll point out that anyone using JSON responses in JavaScript should either parse JSON explicitly or validate the JSON before taking eval() to it. See here:

http://www.JSON.org/js.html

页面链接很棒 参考实施 建议的ECMAScript JSON解析器 接口JSON.parse().

Linked from the page is a great reference implementation of the proposed ECMAScript JSON parser interface, JSON.parse().

欢呼声,天晴

这篇关于使用jQuery获取JSON数据会返回无效的标签错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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