Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

jquery - Basic how-to for cross domain jsonp

I looked everywhere here for this. I need just a simple "how-to" pull jsonp cross domain. 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...