the principle of JSONP is very simple, the main use of HTML all have SRC attributes of the label can cross-domain characteristics, using the script src for GET request, the backend output a section of JS code string in script will be executed. Of course, the back end output normal JSON string is not, the background should output the FN (JSON) style string, fn represents the JS method to invoke; The JSON represents the data to be processed. This is also the reason why Jsonp is named. Backstage will not know which JS to be transferred, So we need the foreground to send the name of the function to be called backstage.
A simple Jsonp example of background node version
var http = require (' http '); var server = new http. Server (); Server.listen (8000); Server.on (' Request ', function (request, response) { var url = require (' URL ') ; var params = Url.parse (Request.url, true). query; var fn = Params.fn; Response.writehead ($, {' Content-type ': ' Text/plain '}); Response.Write (fn+ '); Response.End ();});
Front desk
<! DOCTYPE html>
Jsonp Simple Example