问题
I'm trying to make getJSON to use JSONP object, but I can't figure out how to build the url:
for instance: http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream
where and how should I add "callback=?" parameter?
thank you
d.
回答1:
Append &callback=?
to the URL.
$.getJSON('http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream&callback=?', function(data) {
// ..
});
// Or (more clean):
$.getJSON('http://graph.facebook.com/?callback=?',
{
ids: 'http://legrandj.eu/article/blouse_ghost_dream'
},
function(data) {
// ...
}
);
Given this code, jQuery creates and inserts a <script src="http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream&callback=jQuery171022388557461090386_1332329918803&_=133232991983">
. Explanation of the URL:
- Base URL: http://graph.facebook.com/?ids=http://legrandj.eu/article/blouse_ghost_dream
- Callback handler:
&callback=jQuery171022388557461090386_1332329918803
jQuery replaces?
incallback=?
with an unique temporary identifier. - Cache-breaking:
&_=133232991983
The FB API returns a response in the following format (JSONP):
/**/ jQuery171022388557461090386_1332329918803({
"http://legrandj.eu/article/blouse_ghost_dream": {
"id": "http://legrandj.eu/article/blouse_ghost_dream",
"shares": 3
}
});
Since this is included by the <script>
tag, a function jQuery171022388557461090386_1332329918803
is called, passing the parsed JSON as an argument. The parsed JSON is then passed to the function which you defined in jQuery.getJSON
.
来源:https://stackoverflow.com/questions/9803691/exact-string-to-getjson-and-callback