A simple example of JSONP and a jsonp example
The jsonp principle is very simple. It mainly utilizes the cross-origin features of all the tags with src attributes in HTML and uses the src of script for get requests, the string that outputs a piece of js Code on the backend will be executed in the script. Of course, the backend cannot output common json strings. the backend should output fn (json) style strings. fn indicates the js method to be called. json indicates the data to be processed. this is why jsonp is named. the background does not know which js to call, so the foreground needs to send the name of the function to be called to the background.
A simple jsonp example 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(200, {'Content-type':'text/plain'}); response.write(fn+'(22)'); response.end();});
Front-end
<!DOCTYPE html>
Which eldest brother gives the younger brother a detailed explanation of JSONP and Its Implementation Method? It is best to have a small example, which requires Chinese
Simple javascript example
<Html>
<Head>
<Script language = "javascript">
<! --
Function show ()
{
// Alert(registry.doc ument. myform. username. value );
If (document. myform. username. value = "")
{
Alert ("NO Complete ");
Alert ("4444444444 ");
Return false;
}
Else
{
Alert ("OK ");
Return true;
}
}
// -->
</Script>
</Head>
<Body>
<Form name = "myform" action = "test.html" method = "post">
<Input type = "input" name = "username" value = ""/>
<Input type = "submit" name = "mybutton" value = "click" onClick = "return show ()"/>
</Form>
</Body>
</Html>