JSONP
is a technique to get data from server without cross-domain issues.
JSONP does not use the
XMLHttpRequest
, instead it uses
<script>
tag.
window.onload = function() {
var s = document.createElement("script");
s.src = "https://www.w3schools.com/js/demo_jsonp.php";
document.write(s.outerHTML);
};
// This function will run after the content is fully loaded from the server
function myFunc(myObj) {
document.write(myObj.name + " is " + myObj.age + " and he lives in " + myObj.city);
}
Requesting a content from another domain can cause problems, due to cross-domain policy.
<script>
tag does not have this problem and that is the reason why it uses the tag instead of using
XMLHttpRequest
object.
...
Content of the remote
demo_jsonp.php
file
myFunc({"name":"John", "age":30, "city":"New York"});