Friday, May 11, 2018

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 sript from another domain doe snot have this problem.

Callback Function
when you have no control over the server file, how do you get the server file to call the correct function?
Sometimes the server file offers a callback function as a parameter:

Example

The php file will call the function you pass as a callback parameter:
function clickButton() {
    var s = document.createElement("script");
    s.src = "jsonp_demo_db.php?callback=myDisplayFunction";
    document.body.appendChild(s);
}
Try it Yourself »

No comments:

Post a Comment