开发者

i just can't seem to make json work. full stop. i pretty much copied http://api.jquery.com/jQuery.getJSON/ with no luck

i just can't seem to make json work. full stop. i pretty much copied http://api.jquery.com/jQuery.getJSON/ with no luck


        $("#thumbnails .thumb").find(".viewsCount").each(function(){
        var PostID = $(this).html();
        $.getJSON("http://tom.is-a-geek.org/tumblr/counters/thomee/go.php?c=yeeeboiii&i=" + PostID + "&justCount=y&format=json&jsoncallback=?",function(data){
             $.each(data.items, function(i,item){
                //$(t开发者_如何学编程his).html(item.views); 
                alert(item.views);
                alert("sigh");
            });         
        });
    });

({
    "items": [
     {
        "views": "20"
     }
    ]
})

live @ http://thomee.tumblr.com

any assistance would be appreciated. :-(


You can't do a cross-site getJSON due to security reasons. So either use local (I mean local as in "same domain") url in getJSON or try using a JSONP hack/workaround: http://code.google.com/p/jquery-jsonp/


As František says, cross-site ajax (getJSON is ajax under-the-covers) is restricted by the Same Origin Policy.

You have lots of options, though:

  • As František says, you can use JSON-P if the server in question supports it.
  • If the target server supports CORS and the browser your visitor is using supports it (recent versions), it will work (except that the versions of IE that support CORS — 8 and 9 — requires special handling rather than having it Just Work as it does in Firefox and Chrome).
  • You can use YQL as a proxy.

(This is a CW because it's really just a big adjunct to František's answer. Didn't feel comfortable adding all of this to that answer, although in theory of course that's what StackOverflow is all about, collaboratively answering questions.)


Separately, FYI, the JSON you quoted:

({
    "items": [
     {
        "views": "20"
     }
    ]
})

...is invalid, JSON has no parenthesis operator. That should be:

{
    "items": [
     {
        "views": "20"
     }
    ]
}

The reason you sometimes see parentheses near JSON text is that when evaluating JSON, sometimes people will use the JavaScript parser to parse JSON (since JSON is a subset of JavaScript's object literal notation):

var result = eval("(" + jsonString + ")");

They put the parentheses in to ensure that the contents of the jsonString are evaluated by the parser where an expression is expected. But the parentheses are not part of the JSON and a proper JSON parser will fail if you use the JSON you quoted. (Re this eval technique: Only use it if you know the source of the JSON is trustworthy [your own server, etc.]; otherwise, best to use an actual JSON parser to defend against script injection attacks, the parentheses are not remotely a projection against those. You can find three JSON parsers — one that relies on eval but does some checks first, and two that don't use eval at all — on the github page of Douglas Crockford, inventor of JSON.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜