Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Due to same-origin-policy, XMLHttpRequest is not normally allowed to fetch data from other domains. There are work-arounds such as CORS or using a proxy on your domain or using an embedded flash or java applets.</p> <p>However, JSONP is a different story. That's because JSONP does not technically return data. JSONP returns a javascript file. As such, getting data using JSONP simply requires you to add a script tag to your page:</p> <pre><code>&lt;script src="http://other.server.com/path/to/jsonp/data"&gt;&lt;/script&gt; </code></pre> <p>To do it programmatically:</p> <pre><code>var jsonp = document.createElement('script'); jsonp.src = "http://other.server.com/path/to/jsonp/data"; document.body.appendChild(jsonp); </code></pre> <p>The problem with this is that script tags don't return anything. To solve this, the JSONP protocol passes a function name to the server so that the server will wrap that function around the JSON data.</p> <p>For example, if your regular JSON data looks like this:</p> <pre><code>{"result":"something"} </code></pre> <p>The JSONP equivalent would look something like this:</p> <pre><code>callback({"result":"something"}) </code></pre> <p>So, to take the original example, our new code would now be:</p> <pre><code>function processResult (obj) { console.log(obj); } var jsonp = document.createElement('script'); jsonp.src = "http://other.server.com/path/to/jsonp/data?jsonp=processResult"; document.body.appendChild(jsonp); </code></pre> <p>Notice how we're passing the name of the function to handle the return value in the query param of the URL.</p> <p>Note that while in this example the parameter is "jsonp" the server may implement it using some other name. Another common one is "callback", as in <code>callback=processResult</code>. Read the API documentation of the server you're connecting to.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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