Skip to main content

Basic example of using .ajax() with JSONP?

Please could someone help me work out how to get started with JSONP?

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

Popular posts from this blog

Training PHP JQuery bersama PT.GSP bandung

Terima kasih kami ucapkan kepada peserta dari PT . GSP Bandung yang telah mengikuti Training PHP JQuery di SISINDOTEK Bandung Salam, SISINDOTEK - IT Training & Solution Provider Jl. Sukasenang VI-6B Bandung - Jawa Barat 40124 Tel. 022-71242266 , SMS. 0812.8733.1966 info , YM. sisindotek www.sisindotek.com , facebook.com/sisindotek , twitter.com/sisindotek

Training MS Project Secara Online - Hery Purnama

Home  » » Training Online Microsoft Project dan VBA Macro - Hery Purnama Training Online Microsoft Project dan VBA Macro - Hery Purnama 9:24 AM       No comments Mau tetap bisa training MS Project bersama Hery Purnama ditengah masa Panedemi COVID-19, tidak usah khawatir kini saya membuka kelas webinar  MS Project dan Excel VBA Macro (Senin-Sabtu) dengan 3 slot waktu 09.00-11.30 , 13.00 - 15.30 , 19.30 - 22.00 , book sekarang karena peminat sangat banyak sampai saat ini, hubungi 081-223344-506 Regards, HERY PURNAMA Certified IT Trainer http://www.inhousetrainer.net E-mail :  inhousetrainer@yahoo.com ,  hery.purnama@gmail.com  Call/ SMS/WA :  081.223344.506 , BBM : 7DC633AA      Social Net :   facebook.com/inhousetrainer   twitter.com/inhousetrainer   instagram.com/inhousetrainer Hometown : Bandung - Jawa Barat

WHAT IS COBIT ISACA

What is COBIT? COBIT stands for Control Objectives for Information and Related Technology. It is a framework  created by the ISACA (Information Systems Audit and Control Association). It was designed to be a  supportive tool for managers—and allows bridging the crucial gap between technical issues,  business risks, and control requirements. COBIT is a thoroughly recognized guideline that can be applied to any organization in any  industry. Overall, COBIT ensures quality, control, and reliability of information systems in an  organization, which is also the most important aspect of every modern business. Cybersecurity Expert Master's Program Master the Skills of a Cybersecurity ProfessionalVIEW COURSECybersecurity Expert Master's Program What is ISACA? ISACA stands for Information Systems Audit and Control Association. It develops controls and  guidance for information governance, security, control, and audit professionals. This i...