Java Reference
In-Depth Information
JSON With Padding
JSONP or JSON with padding is a very common technique of transporting data as it allows
the same-origin policy to be bypassed without using CORS. It involves sending a JSON
string wrapped in a callback function that is then injected into a <script> tag. The
same-origin policy doesn't apply to <script> tags, and because the callback function has
already been defined in the domain requesting the data, all the data wrapped in the function
will be available in that domain by referencing the callback function.
Warning: The Server Must Support JSONP
The server responding to the request must support JSONP requests and return
a callback function. The technique will fail to work if normal JSON data is
returned.
JSONP in Action
To demonstrate using JSONP, we'll create another small web page. In a new folder, create a
file called jsonp.htm and add the following HTML code:
jsonp.htm
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSONP Example</title>
</head>
<body>
<button id="send">Request Text</button>
<div id="output">
Ajax response will appear here
</div>
<script src="scripts.js"></script>
</body>
</html>
Search WWH ::




Custom Search