HTTP by <script>: JSONP

The introduction to this chapter mentioned that a <script> element can be used as an Ajax transport mechanism: simply set the src attribute of a <script> (and insert it into the document if it isn’t already there) and the browser will generate an HTTP request to download the URL you specify. <script> elements are useful Ajax transports for one primary reason: they are not subject to the same-origin policy, so you can use them to request data from servers other than your own. A secondary reason to use <script> elements is that they automatically decode (i.e., execute) response bodies that consist of JSON-encoded data.

The technique of using a <script> element as an Ajax transport ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.