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

How to Pass ISACA CGEIT Certification Exam Dumps Practice Test

ISACA CGEIT certification is mainly targeted to those candidates who want to build their future in IT Governance domain. CGEIT exam can provide those within an information technology related governance field with great benefits, both with potential employers, and unlock a network of like-minded individuals. ISACA's Certified in the Governance of Enterprise IT (CGEIT) exam certification is framework agnostic and the only IT governance certification for the individual. CGEIT can put you in the role of a trusted advisor to your enterprise. CGEITs maintain an adequate level of current knowledge and proficiency in the field of information systems audit, control and security. The technical skill requirement of CGEIT is that the candidate has relevant professional work experience supporting organizational enterprise information technology. CGEIT is a vendor-neutral enterprise IT governance certification which can help take your career to new heights. Free Exam Practice Question and Answer...

Jual Rumah di Prapanca Raya

Jual Rumah di Jalan Utama Prapanca Raya Rumah yang berlokasi di jalan utama Prapanca Raya ini sangat cocok digunakan selain sebagai hunian dapat juga digunakan untuk Cafe / Restaurant atau menjadi kantor. Bangunan dan struktur rumah sangat terawat rapih karena selalu ditempati keluarga Adapun spesifikasi singkat sebelum anda serius untuk mengetahui detail lebih lanjut dengan menghubungi kami adalah : Heritage house Luas Tanah :  378 m2 Luas Bangunan : 200m2 (dimana Lebar bangunan belakang lebih besar dari bangunan depan - ngantong Sertifikat Hak Milik (SHM) Listrik 10,000watt Telp 2 line Jumlah Lantai : 1 4 km tidur 2 km mandi + 1 km tidur & km mandi pembantu . Rumah 1 lantai. Air : Sumur pompa & PDAM Harga : Rp 29,9 Milyar (Tanpa Perantara) Rumah kami jual tanpa perantara , dan dengan segala hormat mohon untuk tidak menjadikan iklan ini sebagai peluang untuk menjadi perantara karena kami menginginkan proses tawar menawar dan transaksi penjualan rumah yang penuh i...

ISO 27001 Trainer pelatihan training di Bali, Jayapura 2018

ISO 27001   Trainer Mr. HERY PURNAMA – 081.223344.506   www.inhousetrainer.net   adalah #trainer #freelance 2018     # Bali, Jayapura   untuk training #pelatihan   ISO 27001   terkenal di indonesia dengan pengalaman lebih dari 17 tahun di bidang IT dan sertifikasi internasional , selain corporate trainer Hery Purnama juga terkenal sebagai IT Enterpreneur , pengusaha dan Motivator Bisnis (motivational speaker). Topik training lainnya     yang Hery Purnama 081-223344-506   www.inhousetrainer.net   kuasai antara lain   sebagai trainer     ISO 27001   ,   ITIL , CISA, PMP, PMBOK , PMO, COBIT, EDMS, BABOK, DMBOK, SAD UML 2.0, SQA, PRINCE2, CRISC, TOGAF, ISTQB, RDBMS Concept, e-LMS, KMS, SCM, TRAINER ISO 27001, ISO 9001, ISO 20000, ISO 38500, INDEKS KAMI, SEO Internet marketing, IIOT, SIX SIGMA, APACHE HADOOP SPARK, TRAINER BIG DATA, DBA ORACLE SQL SERVER MYSQL MARIADB POTGRESQL, MICROSOFT PROJECT, PRIMAVERA P6, PROJECT LIBRE, QGIS, ARCGIS, ASP NET, TRAINING VB NET, PHP, JSON, XML. SOAP,...