Cross-origin request

Source: Internet
Author: User
XMLHttpRequest cannot loadhttp://localhost/MongoTest/person.php. Origin http://localhost:81 is not allowed by Access-Control-Allow-Origin.

Cross-origin request rejected due to same-origin policy

Solution:

1. Use the <SCRIPT> tag

The browser will block Ajax requests from non-same-source content, but there is no restriction on the <SCRIPT> tag to access non-same-source content, and there is no restriction on dynamic addition of the <SCRIPT> tag, so we can use this method to break this restriction.

<script type="text/javascript" src="http://localhost:80/MongoTest/person.php" ></script>

Dynamic addition: Creates a script tag, assigns a value to SRC, and adds it to the HTML document through DOM operations.

2. jsonp

Jsonp (JSON with padding) is a protocol used by the server and client to collaborate with each other to complete cross-origin requests. The client sends requests to the server with the callback function attached, the server returns the corresponding JS Code. This code is used to execute the callback function, and the parameter is the JSON data returned by the server. When a client initiates a cross-origin request, a specific callback function is required. For example, the request http: // localhost: 80/each test/person. php? Callback = sayhello, callback is the callback function, and the server also needs to extract the callback function name through callback. Therefore, the specific parameter must be consistent between the client and the server; otherwise, cross-origin cannot be implemented. Use jquery to implement jsonp
$. Ajax ({type: "Get", async: false, URL: "http://www.xxx.com/ajax.do", datatype: "jsonp", jsonp: "callbackparam ", // The parameter jsonpcallback of the function name used by the server to receive callback calls: "success_jsonpcallback", // The function name success of callback: function (JSON) {alert (JSON ); alert (JSON [0]. name) ;}, error: function () {alert ('fail ');}});
public void ProcessRequest (HttpContext context) {    context.Response.ContentType = "text/plain";    String callbackFunName = context.Request["callbackparam"];    context.Response.Write(callbackFunName + "([ { name:\"John\"}])");}

 

 

Cross-origin request

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.