开发者

jQuery JSONP call working for other sites, but not mine

I am working on a little bookmarklet, and hence the calls back to my site will need to be done via JSONP to work around the cross-domain restrictions. I have included a couple of demos below so you can see exactly what I am talking about.

I have everything up and running just fine with a JSON source provided on Twitter's site (my bookmarklet has nothing to do with Twitter, just that I can get it to work using their JSON data). However, I can't seem to make it work with JSON coming from my server.

To make it dead simple, I took the exact response I am receiving from Twitter's JSON call and put 开发者_StackOverflow社区it into a JSON file on my own server. Even by calling against the exact JSON data, when the JSON comes from my server, it fails to load.

Given this, I seem to have ruled out: improper JSON and my JavaScript / jQuery code (again, works when it comes from somewhere else).

It leads me to think that there is something about the way my server is handing back the JSON file that jQuery does not like. I have checked and the JSON coming from my server is set with the appropriate content type ("application/json").

Here are some samples so you can see exactly what I am seeing. Does anyone have any ideas?

Again, to underscore, the two samples below are the same code, just calling to the same JSON results from different places. Banging my head against the wall here.

Thanks for any help or advice.

Working Demo:

http://www.hallsnet.com/temp/working.htm (see /temp/working.js for the JavaScript source)

Non-working Demo:

http://www.hallsnet.com/temp/not-working.htm (see /temp/not-working.js for the JavaScript source)


Your json file does not call any function while jQuery is expecting for your json to dynamically call a function depending on which name jQuery assigns to it (as you use callback=? in the url).

If you look at the twitter json file, it adds the data into a function name based on what the callback parameter in the url is set to, for example callback=asd will call the data in the json file with a function name asd:

http://twitter.com/status/user_timeline/smith.json?count=10&callback=asd

asd([{"geo":null,"use... etc...

In your json file:

http://www.hallsnet.com/temp/not-working.json?callback=saddas32

you are not wrapping your data inside any function callback (and jQuery is dynamically creating these as you haven't specified a static one to use), so the json file isn't actually calling any function, and jQuery is just waiting there endlessly for it to do so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜