JSONP
JSONP is a method for sending JSON data without worrying about cross-domain issues.
JSONP does not use the XMLHttpRequest object.
JSONP uses the <script> tag instead.
JSONP Intro
JSONP stands for JSON with Padding.
Requesting a file from another domain can cause problems, due to cross-domain policy.Requesting an external script from another domain does not have this problem.
JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.
<script src="demo_jsonp.php">
The Server File
The file on the server wraps the result inside a function call:Example
<?php
$myJSON = '{ "name":"John", "age":30, "city":"New York" }';
echo "myFunc(".$myJSON.");";
?>
Make sure that the function exists on the client.
The JavaScript function
The function named "myFunc" is located on the client, and ready to handle JSON data:Example
function myFunc(myObj) {
document.getElementById("demo").innerHTML = myObj.name;
}