Installation (requires node)
# install the sandbox
git clone https://github.com/jordanpatton/jsonp-sandbox.git
cd jsonp-sandbox
npm install
# start the server
node server.js
# start the client (in new terminal session)
node client.js
- Open the client.
- Open your browser console and inspect your network traffic.
- Click the
Request
button.
The following things will happen very quickly when you click Request
:
- Browser adds a
script
node to the DOM. - Browser requests a cross-origin file from the server.
- Server generates the script dynamically.
- Browser loads the generated script.
- Browser parses the generated script and invokes the embedded callback.
- Browser inserts the result in the lower
textarea
. - Browser removes the
script
node from the DOM.
It's likely this will happen so quickly that you won't even see the DOM transform.
Q: Why would I ever use this?
A: JSONP provides a viable alternative to cross-origin resource sharing.