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

Sewa Ruang Meeting Daerah Citarum Bandung

Sewa ruang meeting kantor, training di Bandung Hubungi 0816-4038-78. Lokasi Ruang Meeting berada di Jl.Pelajar Pejuang 45 No.23B 1. Paket per Jam      - Biaya : Rp.50.000 / org / jam     - Fasilitas : Snack & Drink 2. Paket Half-Day     - Waktu : 09.00 s/d 13.00 Wib     - Biaya : Rp.120.000 / org      - Fasilitas : Makan siang 1x, Snack & Drink 3. Paket Full Day     - Waktu : 09.00 s/d 16.00 Wib     - Biaya : Rp.170.000 / org      - Fasilitas : Makan siang 1x, Coffe break 2x, Snack & Drink 4. Lain-lain     - Sewa Proyektor : Rp.150.000/day     - Extra time : Rp.100.000/Jam Syarat dan Ketentuan : 1. Min. 3org dan Max. 7org 2. Pembayaran H-3 sebelum tgl pelaksanaan 3. Melebihi jam ketentuan dikenakan biaya tambahan Sewa Ruang Meeting Daerah Citarum Bandung Hubungi 0816-4038-78 Ruang meeting murah mulai 50.000 di Buah Batu, Pasteur, Sukajadi, Lengkong, Antapani, Uj...

Cara Aktifasi Paket Internet Perdana Simpati 2,5GB

Paket internet flash perdana telkomsel flash discovery 2,5 GB lumayan cepat. Pengalaman ini mungkin bisa menjadi pelajaran buat netizen. Sebelumnya saya biasa menggunakan Axis lalu lanjut ke flash, suatu ketika coba menggunakan layanan simpati perdana yang dibeli di counter dekat rumah dan alhasil super kencang, kejadian unik terjadi setelah paket habis maka selanjutnya seperti yang biasa saya lakukan dengan simcard internet lain yaitu membuangnya dan membeli paket internet perdana baru, yang tidak saya sadari sebelumnya adalah waktu membeli paket perdana yang pertama adalah paket sudah aktif, tinggal pasang simcard di modem dan langsung jalan. Namun untuk pembelian yang ke2 ini saya salah besar ternyata perdana yang saya beli belum aktif (walau kata si mas yang jual sudah aktif dan langsung pake). Maka selanjutnya yang saya lakukan sudah bisa Anda tebak, saya masukkan ke modem dan sinyal tidak muncul serta muncul permintaan registrasi, OK no problem (berarti si masnya salah) saya la...

Promo DAFTAR AGEN GRATIS

Kabar gembira !Kami membuka promo pendaftaran AGENRUMAH123 gratis untuk 20 pendaftar yang bergabung melalui http://www.agenrumah123.com/agenrumah123 hanya untuk yang bergabung melalui link agen http://www.agenrumah123.com/agenrumah123 , klik / akses segera link tersebut jangan sampai tertinggal dan segera posting iklan properti Anda tanpa batas waktu, jumlah iklan dan gambar Supported by SISINDOTEK - I.T Training & Solution Provider Jl. Sukasenang VI - 6 B Cikutra Bandung Jawa Barat 40124 Tel. 022-71242266, 0812.8733.1966 Email : info@sisindotek.com , YM. sisindotek www.sisindotek.com , facebook.com/sisindotek , twitter.com/sisindotek