Please could someone help me work out how to get started with JSONP?
Code:
Fiddle: http://jsfiddle.net/R7EPt/6/
Should produce an alert, as far as I can work out from the documentation: isn't (but isn't producing any errors either).
thanks.
ANSWER :
JSONP is really a simply trick to overcome XMLHttpRequest same domain policy. (As you know one cannot send AJAX (XMLHttpRequest) request to a different domain.)
So - instead of using XMLHttpRequest we have to use script HTMLl tags, the ones you usually use to load JS files, in order for JS to get data from another domain. Sounds weird?
Thing is - turns out script tags can be used in a fashion similar to XMLHttpRequest! Check this out:
You will end up with a script segment that looks like this after it loads the data:
However this is a bit inconvenient, because we have to fetch this array from script tag. So JSONP creators decided that this will work better (and it is):
Notice *my_callback* function over there? So - when JSONP server receives your request and finds callback parameter - instead of returning plain JS array it'll return this:
See where the profit is: now we get automatic callback (*my_callback*) that'll be triggered once we get the data. That's all there is to know about JSONP: it's a callback and script tags.
Code:
$('document').ready(function() { var pm_url = 'http://twitter.com/status'; pm_url += '/user_timeline/stephenfry.json'; pm_url += '?count=10&callback=photos'; var photos = function (data) { alert(data); }; $.ajax({ url: pm_url, dataType: 'jsonp', jsonpCallback: 'photos', jsonp: false, }); });
Fiddle: http://jsfiddle.net/R7EPt/6/
Should produce an alert, as far as I can work out from the documentation: isn't (but isn't producing any errors either).
thanks.
ANSWER :
JSONP is really a simply trick to overcome XMLHttpRequest same domain policy. (As you know one cannot send AJAX (XMLHttpRequest) request to a different domain.)
So - instead of using XMLHttpRequest we have to use script HTMLl tags, the ones you usually use to load JS files, in order for JS to get data from another domain. Sounds weird?
Thing is - turns out script tags can be used in a fashion similar to XMLHttpRequest! Check this out:
script = document.createElement(”script”); script.type = “text/javascript”; script.src = “http://www.someWebApiServer.com/some-data”;
You will end up with a script segment that looks like this after it loads the data:
<script> {['some string 1', 'some data', 'whatever data']} </script>
However this is a bit inconvenient, because we have to fetch this array from script tag. So JSONP creators decided that this will work better (and it is):
script = document.createElement(”script”); script.type = “text/javascript”; script.src = “http://www.someWebApiServer.com/some-data?callback=my_callback”;
Notice *my_callback* function over there? So - when JSONP server receives your request and finds callback parameter - instead of returning plain JS array it'll return this:
my_callback({['some string 1', 'some data', 'whatever data']});
See where the profit is: now we get automatic callback (*my_callback*) that'll be triggered once we get the data. That's all there is to know about JSONP: it's a callback and script tags.
Comments
Post a Comment
Silahkan isi komentar atau iklan baris Anda, Jangan lupa visit social media kami di FB/Twitter/Instagram @alamatclick