使用jQuery获取json数据会返回无效的标签错误

前端之家收集整理的这篇文章主要介绍了使用jQuery获取json数据会返回无效的标签错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个代码,并且还使用$.getJson函数尝试了类似的东西:
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中的结果.有人能指出我应该做的一个例子吗?

Firebug错误

invalid label
07000
Line 1

可以在此处找到json外观的示例输出
http://build.kiva.org/docs/data/loans

解决方法

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

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

…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:

07001

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.

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 (
07002
07003 ).
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.

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:

07004

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

Cheers,skylar

猜你在找的jQuery相关文章