How JavaScript implements Cross-domain request _javascript tips

Source: Internet
Author: User

What is a cross-domain request? The simple understanding of
is to make a request to a server file that is not in the same domain name. This is still a practical example to explain it, such as baidu.com to cxyblog.com send a request, the two domain name is different, then this is cross-domain, for security reasons, this is not allowed. Also need to note that different subdomains or the same domain name between different ports or the same domain name of different protocols to send requests are also cross-domain, basically can be grouped into the following categories:
(1) http://www.baidu.com to http://www.cxyblog.com sends the request
(2) http://www.cxyblog.com to http:// image.cxyblog.com Send Request
(3) http://www.baidu.com:8000 to http://www.cxyblog.com Send Request
(4) http://www.cxyblog.com to https://www.cxyblog.com send Request
(5) http://www.cxyblog.com to http://112.65.242.67 Sending a request (assuming that the IP of the domain name www.cxyblog.com is 112.65.242.67)
All five of these situations are cross-domain requests.

When will cross domain requests be used?
There are times when we need to use JavaScript for AJAX operations to run into this Cross-domain request operation problem.
Why is it not possible to use JavaScript directly to implement Cross-domain requests?
For security reasons, the browser does not allow JavaScript to request Cross-domain Resources because of the javacript of the homologous policy.
How do I solve the problem that JavaScript does not implement Cross-domain requests?
The workaround used in this article is to implement Cross-domain requests using the FLYJSONP application Jsonp. Flyjsonp is a lightweight JavaScript class library, also known as the JSON plug-in, with a total size of about 3KB after compression, without the need for other frameworks.
FLYJSONP official website address:http://alotaiba.github.com/FlyJSONP/
So how do you use the Flyjsonp class library to implement Cross-domain requests?

(1) First load the Flyjsonp JavaScript script, namely:

<script language= "javascript" src= "Http://www.cxyblog.com/flyjsonp.min.js" ></script>

(2) Then to initialize the instance of FLYJSONP, the parameter debug can be set to TRUE or false, that is, Flyjsonp.init ({debug:true}), which means whether to open debug information, the parameter value is true or false;

(3) The next is to use the Get method or Post method request data, the specific example code is as follows:

FLYJSONP implements Cross-domain get
function GetData () {
 flyjsonp.init ({debug:true});//Initializes an instance of Flyjsonp, Parameter debug can be set to TRUE or False
 flyjsonp.get {
  URL: ' Http://www.cxyblog.com/article.json ',//URL
  of the target request parameters:{//request parameter
   limit:5
  },
  success:function (data) {//Send request successfully
   Console.log (data);
  Error:function (errormsg) {//Send request failed
   Console.log (errormsg);}}
 );
FLYJSONP implements the Cross-domain post
function postdata () {
 flyjsonp.init ({debug:true});//Initializes an instance of Flyjsonp, Parameter debug can be set to TRUE or False
 flyjsonp.post ({
  URL: ' http://www.cxyblog.com/article/new ',
  parameters:{
   Username: ' Cxyblog ',
   api_key: ' 123456 ',
   title: ' Flyjsonp ',
   Description: ' Test '
  },
  success : function (data) {
   alert (data);}}
 );

Note: When the server is output to the client, the output must be a JSON string or the client will not be able to receive it.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.