Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Cross-site XMLHttpRequests can only be done in browsers that implement the W3C <a href="http://www.w3.org/TR/cors/" rel="noreferrer">Cross-Origin Resource Sharing</a> spec and if the server returns the appropriate access control headers (see <a href="https://developer.mozilla.org/En/HTTP_access_control" rel="noreferrer">MDC article</a>), e.g.:</p> <p><code>Access-Control-Allow-Origin: *</code></p> <p>But this is not implemented by all browsers. The only sure-fire way to do cross-site requests is to use <a href="http://en.wikipedia.org/wiki/JSON#JSONP" rel="noreferrer">JSONP</a>, for (untested) example:</p> <pre><code>(function() { function shorten(ref, callback){ var callbackFuncName = 'esvapiJSONPCallback' + (new Date()).valueOf(); var script = document.createElement('script'); script.type = "text/javascript"; script.src = "http://esv.to/api/" + escape(ref) + "?callback=" + callbackFuncName; window[callbackFuncName] = function(shorturl){ script.parentNode.removeChild(script); window.callbackFuncName = null; delete window[callbackFuncName]; callback(shorturl); }; document.getElementsByTagName("head")[0].appendChild(script); } var ref = prompt("Enter a scripture reference or keyword search to link to:", ""); shorten(ref, function(shorturl) { prompt("Here is your shortened ESV URL:", shorturl); }); })(); </code></pre> <p>When the server sees the <code>callback</code> parameter it would then need to return <code>text/javascript</code> instead of <code>text/plain</code>, and the response body would need to be wrapped in an invocation of the provided callback, for example:</p> <pre><code>&lt;?php #... after $shorturl is set ... if(isset($_GET['callback'])){ header('Content-Type: text/javascript'); $callback = preg_replace('/\W+/', '', $_GET['callback']); #sanitize print $callback . "(" . json_encode($shorturl) . ");"; } else { header("Content-Type: text/plain"); print $shorturl; } ?&gt; </code></pre>
 

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