Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should look up JSONP. What you want your script to return is the invocation of a callback with the data as the argument.</p> <pre><code>&lt;script id="scr1" src="http://www.example.com/some/action?callback=cb" type="text/javascript&gt;&lt;/script&gt; </code></pre> <p>The server side then, would produce the script contents:</p> <pre><code>cb( ["hello", "world", "what", "where"] ); </code></pre> <p>Then on in your page, you'd need to have a function named <code>cb</code> that expects an array.</p> <pre><code>function cb(data) { // do something with the array } </code></pre> <p>Of course, this expects the cooperation of the server to understand your callback parameter and respond appropriately. If you control the server, that's pretty easy to do. If not, then you'll need to use an API that is constructed to return JSONP. The <code>callback</code> parameter is a pretty standard name for such frameworks. If they don't have an API, then you need to proxy the request on your server, in which case you can use standard JSON.</p> <p>FWIW, jQuery makes this pretty easy for APIs that expect to return JSONP.</p> <pre><code>$.getJSON( 'http://example.com/api/method?callback=?', data, function(result) { // do something with the json result }); </code></pre> <p>You can also use this with post/get/ajax methods as well, but <a href="http://api.jquery.com/jQuery.getJSON/" rel="noreferrer">getJSON</a> is easier. Note that including the callback parameter automatically tells it to use a JSONP (script) request and automatically fills in the name of the callback it creates to wrap the (anonymous) callback function in the request.</p>
 

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