开发者

Basic how-to for cross domain jsonp

I looked everywhere here for this. I need just a simple "how-to" pull jsonp cross domain. 开发者_JS百科I'm using jQuery 1.5.1.

I tried the following in a program on another site:

$.getJSON("http://www.mydomain.com/testjson.json?jsoncallback=?", function(data) {
    alert("I'm hitting this.");
}

This doesn't work at all.

Is there a way of just doing a simple cross domain jquery JSONP call?

Thanks


JSONP requires the cooperation of the server to succeed. You cannot pull random pages using JSONP and expect them to succeed; the server needs to know:

  1. It needs to formulate a JSONP response, rather than a JSON response.
  2. It needs to know the name of the function to wrap the response around.

If you're unsure on why the server needs to know these, or what the differences are between JSON and JSONP, you should read up on them; or the whole thing will make no sense. For starters, check out Can anyone explain what JSONP is, in layman terms? and http://en.wikipedia.org/wiki/JSONP.

After understanding this a little more, you'll probably find the server is returning

{ "key": 1, "bar": "foo" }

(which is valid JSON), rather than:

someCallback({ "key": 1, "bar": "foo" })

which is a JSONP response.


If you try these http://terrasus.com/detail.jsp?articleID=396 step by step it will work fine. if you produce jsonp response you should get the callback value and set it to your response dynamically. This article has a detail explanation

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜