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 SQL Server 2017 DATABASE ADMINISTRATOR-BANDUNG

TRAINING SQL Server 2017 DATABASE ADMINISTRATOR By Purnama Academy - Training Center January 18, 2018  No comments SQL Server 2017 DATABASE ADMINISTRATOR Syllabus Class Type           : Training Duration              : 4 Days (09.00 – 17.00) Training Description : Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network Database administrators (DBAs) use specialized software to store and organize data. The role may include capacity planning, installation, configuration, database design, migration, performance monitoring, security, troubleshooting, as well as backup and data recovery Call us for detail syllabus ! ...

Magento 2 , Using SMTP Gmail

Using SMTP GMAIL in Magento 2.1.0 Tidak seperti Magento Release 1.x dimana Admin bisa me-reset password customer secara manual maka  Ketika Anda menggunakan Magento release 2 anda akan menemukan masalah besar saat Anda membuat customer baru dikarenakan mekanisme di magento 2 tidak memungkinkan lagi buat Anda sebagai Admin untuk men-generate password secara manual. Mungkin ketika web magento 2 anda sudah go live tidak masalah karena sudah terhubung dengan mail server yang secara otomatis menggunakan  function php mail yang ada dimagento untuk menggunakan SMTP server host Anda, Namun jika Anda masih belajar dan masih menggunakan localhost maka tentu saja ini akan jadi masalah. di magento 2 memang tersedia konfigurasi untuk setting SMTP ( Stores -> Configuration -> Advanced -> System - Mail Sending Seeting) namun fitur ini tersedia dengan mode no authentication , kondisi ini tidak memungkinkan kalau Anda menggunakan SMTP Gmai...

Laravel Training Purnama Academy

Nov 2018 Purnama Academy 0838-0838-0001 Kursus Laravel Training Center INdonesia, jakarta, Bandung, Bogor, Semarang, Yogya, Palembang kalimantan, surabaya visit www.purnamaacademy.com , Laravel - Online Courses, Classes, Training, Tutorials on Lynda https://www.lynda.com/Laravel-training-tutorials/2779-0.html Learn how to use Laravel, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Top Laravel Courses Online - Updated November 2018 | Udemy https://www.udemy.com/topic/laravel/ Whether you're interested in developing web apps in the Laravel PHP framework, or building a RESTful API with Laravel, Udemy has a course to help you ... PHP Laravel Framework | Brainmatics.Com brainmatics.com/php-laravel-framework/ Translate this page Laravel merupakan framework PHP yang dirancang untuk membangun situs web ... Pada training ini diharapkan mampu membantu meningkatkan kemampuan ... Dec 3 - Dec 6 Brainmatics Laravel training - Laracasts htt...