Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>JSONP</strong> is really a simply trick to overcome <strong>XMLHttpRequest</strong> same domain policy. (As you know one cannot send <strong>AJAX (XMLHttpRequest)</strong> request to a different domain.)</p> <p>So - instead of using <strong>XMLHttpRequest</strong> we have to use <strong>script</strong> HTMLl tags, the ones you usually use to load JS files, in order for JS to get data from another domain. Sounds weird?</p> <p>Thing is - turns out <strong>script</strong> tags can be used in a fashion similar to <strong>XMLHttpRequest</strong>! Check this out:</p> <pre><code>script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://www.someWebApiServer.com/some-data"; </code></pre> <p>You will end up with a <strong>script</strong> segment that looks like this after it loads the data:</p> <pre><code>&lt;script&gt; {['some string 1', 'some data', 'whatever data']} &lt;/script&gt; </code></pre> <p>However this is a bit inconvenient, because we have to fetch this array from <strong>script</strong> tag. So <strong>JSONP</strong> creators decided that this will work better (and it is):</p> <pre><code>script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://www.someWebApiServer.com/some-data?callback=my_callback"; </code></pre> <p>Notice <em>my_callback</em> function over there? So - when <strong>JSONP</strong> server receives your request and finds callback parameter - instead of returning plain JS array it'll return this:</p> <pre><code>my_callback({['some string 1', 'some data', 'whatever data']}); </code></pre> <p>See where the profit is: now we get automatic callback (<em>my_callback</em>) that'll be triggered once we get the data. That's all there is to know about <strong>JSONP</strong>: it's a callback and script tags.</p> <hr> <p><strong>NOTE:<br> These are simple examples of JSONP usage, these are not production ready scripts.</strong></p> <p><strong>RAW JavaScript demonstration (simple Twitter feed using JSONP):</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;div id = 'twitterFeed'&gt;&lt;/div&gt; &lt;script&gt; function myCallback(dataWeGotViaJsonp){ var text = ''; var len = dataWeGotViaJsonp.length; for(var i=0;i&lt;len;i++){ twitterEntry = dataWeGotViaJsonp[i]; text += '&lt;p&gt;&lt;img src = "' + twitterEntry.user.profile_image_url_https +'"/&gt;' + twitterEntry['text'] + '&lt;/p&gt;' } document.getElementById('twitterFeed').innerHTML = text; } &lt;/script&gt; &lt;script type="text/javascript" src="http://twitter.com/status/user_timeline/padraicb.json?count=10&amp;callback=myCallback"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><br> <strong>Basic jQuery example (simple Twitter feed using JSONP):</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $.ajax({ url: 'http://twitter.com/status/user_timeline/padraicb.json?count=10', dataType: 'jsonp', success: function(dataWeGotViaJsonp){ var text = ''; var len = dataWeGotViaJsonp.length; for(var i=0;i&lt;len;i++){ twitterEntry = dataWeGotViaJsonp[i]; text += '&lt;p&gt;&lt;img src = "' + twitterEntry.user.profile_image_url_https +'"/&gt;' + twitterEntry['text'] + '&lt;/p&gt;' } $('#twitterFeed').html(text); } }); }) &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id = 'twitterFeed'&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><br> <strong>JSONP</strong> stands for <strong>JSON with Padding</strong>. (very poorly named technique as it really has nothing to do with what most people would think of as “padding”.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload