A simple example of JSONP and a jsonp example

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.