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

SEO , Top Keyword Research 2015

Google's Hummingbird update created a lot of anxiety, but ultimately, it could be a good thing for the industry, because it frees us from the tyranny of competing for a limited number of top keywords. Essentially, the role of the Hummingbird algorithm is to better answer those longer-tail queries users are typing in Google. If your pages are optimized for these more conversational queries, you have a better chance of top rankings. Try a new, niche-based approach to keywords, which allows you to double or even triple the list of profitable keywords in your SEO arsenal. This article explains the four steps for doing keyword research the modern way, using SEO PowerSuite or other tools. 1. Ideas: Most search marketers simply think of the main keywords related to their businesses, plop them into a tool like Google Keyword Planner, and then run with the keyword list it delivers. However, search habits vary widely: Searchers may use hundreds of different word combinations to describe the ...

Training Search Engine Optimization (SEO) and Internet Marketing

PURNAMAACADEMY.com (0838-0838-0001) info Jadwal silabus : TRAINING SEARCH ENGINE OPTIMIZATION (SEO) AND INTERNET MARKETING (3 HARI), Kegiatan pelatihan/ training akan dilaksanakan selama 3 Hari, dengan pilihan lokasi Jakarta, Bandung , Surabaya, Yogya dan Bali. Purnama Academy Training (0838-0838-0001) merupakan salah satu penyelenggara training IT dan Manajemen terbaik di Bandung Jakarta Surabaya dan Bali serta beberapa kota lainnya, selain topik training diatas Anda juga bisa mengikuti beberapa pelatihan populer kami lainnya seperti Excel VBA Macro, Leadership, BUsiness Analisys , UML, Software Testing, Autocad, Google Map API, Power BI for Business User, ITIL foundation, COBIT 5 Foundation, Yii Framework, Laravel , Microsoft Project, Primavera dan Magento. Kegiatan Training tersedia atas 3 Paket (Full Day : 09.00 - 16.00, Night Class : 16.30 - 21.00, Weekend Class : 09.00 - 21.00). Hubungi tim Purnama Academy untuk informasi lebih detail terkait silabus, waktu pelaksanaan dan biaya...

TRAINING UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE-BANDUNG

TRAINING UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE By Purnama Academy - Training Center January 08, 2018  No comments UNITY 3D GAME ADVANCED : WORKING WITH FIREBASE www.purnamaacademy.com , Hotline (Call/SMS/WA) :  0838-0838-0001 Syllabus Overview Training yang membahas tingkatan lanjut dari pengembangan game 3D menggunakan UNITY 3D baik skala desktop ataupun mobile, dimana peserta mengetahui cara cara menyimpan informasi data game dari player ke dalam backend dengan bantuan firebase SDK, hal ini memungkinkan untuk pengembangan game skala jaringan (online) Class Type : Training Duration : 2 Days (09.00 – 17.00) Venue / Price : Click Here , Registration : Click Here  (Save up to 20% for Early Bird Registration !) Description : Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business A dependable backend is a must-have for today's games. Giving users the ability to log in and save and retrieve player data can make or...