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

mybabycute.com , Caterpillar 3D Jibbitz Rp 17.500

Caterpillar 3D Jibbitz Rp 17.500 Iklan Jual - AKSESORIS , Jawa Barat Toko : www.mybabycute.com/akudanmama Spesifikasi Sifat Penawaran Harga Sesuai harga pasaran Jumlah yang dijual 1 Kondisi Layak Guna 100% Baru/ Second Baru Ketersediaan Kemasan/ Dus Ada Ketersediaan Garansi Tidak Ada Original Original Harga Pasaran 22500 , Jual menjadi Rp 17.500 Keterangan lainnya New Product ! Keterangan : Caterpillar 3D Jibbitz adalah pin untuk dekorasi sepatu Crocs. Terdiri dari 1 buah bagian, dengan 2 kancing, bentuk 3 dimensi. Tinggi 1,5 cm, Panjang 4 cm. Bahan : Rubberized Plastic Harga : Rp 22.500,- ==>SALE Rp 17.500,- Penjual : Aku dan Mama Telepon : 0856 211 0000 / 022 91211166 Email : info@akudanmama.com Kontak Detail Nama Kontak akudanmama.com No Kontak Telepon / HP 0856 211 0000 / 022 91211166 Lokasi kontak Jawa Barat, Bandung

Cara Pesan Pijat Panggilan di www.terapis.club , Pijat disekitar , bukan pijat plus plus jakarta

via IFTTT

Apakah CTFL ISTQB - Trainer Hery Purnama

Sertifikasi CTFL (Certified Tester Foundation Level) dari ISTQB (International Software Testing Qualifications Board) adalah sertifikasi yang ditujukan untuk menguji pengetahuan dasar seseorang dalam bidang pengujian perangkat lunak. Sertifikasi ini menguji pemahaman Anda tentang konsep-konsep penting dalam pengujian perangkat lunak, seperti pengujian fungsional dan non-fungsional, pengujian perangkat lunak berbasis risiko, dan manajemen pengujian perangkat lunak. Sertifikasi CTFL adalah sertifikasi pengujian perangkat lunak yang paling banyak diakui di seluruh dunia dan dapat membantu meningkatkan kredibilitas dan kemampuan seseorang sebagai seorang profesional dalam industri pengujian perangkat lunak. Undangan Mengajar CTFL ISTQB hubungi Trainer Hery Purnama (081-223344-506) https://inhousetrainer.net