Sinsing Analysis of cors solution for cross-domain transmission

Source: Internet
Author: User

First, we have a concept, that is, the "homologous principle", that is, same-origin policy, which requires a Web site (protocol + host + port number) to determine the script, XMLHttpRequest and WebSocket not authorized to access the content of another site.

If it is not set correctly, it will usually get an error as follows: No ' Access-control-allow-origin ' header is present on the requested resource.

The so-called Cors, also known as Cross-origin Resource sharing, is the rationale for telling the browser that the request is legitimate by setting the header in the HTTP request and return. This involves both the server and browser settings: the initiating of the request (HTTP request header) and the server's correct response to the request (HTTP Response header).

We can use native JavaScript to initiate requests via XMLHttpRequest or xdomainrequest, and we can initiate XHR or cors requests via jquery's $.ajax (). However, this method does not support the xdomainrequest under IE, we need additional plugins to solve this problem.

Examples of breast cancer which are called through jquery:

$.ajax ({

Type: ' GET ',

URL: ' http://www.aaa.com ',

ContentType: ' Text/plain ',

Success:function () {},

Error:function () {}

}});

Depending on the content of the request, the browser will need to add the corresponding header or initiate additional requests. The details are handled by the browser and will be transparent to the user.

In general, we divide cors into two categories: (1) Simple requests (2) not so simple requests

The so-called simple request, that is, the request type must be get, POST, head three, the request header can only contain: Accept, accept Language, content Language, last Event ID, content Type, and the content type accepts only three application/x-www-form-urlencoded, Multipart/form-data, Text/plain.

Except for the above conditions, such as put, delete, or content type Application/json, are "not so simple requests". This request browser will initiate an additional request of type options before the actual request (preflight Request), only if the server responds correctly to the options request, does the browser initiate it.

For a simple request, here is an example of a GET request that B.Com initiated to a.com:

Get/xin   http/1.1origin:http://b.comhost:a.comaccept-language:en-usconnection:keep-aliveuser-agent:mozilla/ 5.0 ...
Before the response, where our head should be set, here's an example:

Access-control-allow-origin:http://b.comaccess-control-allow-credentials:trueaccess-control-expose-headers: foobarcontent-type:text/html; Charset=utf-8

Where Access-control-allow-origin is a list of domain names that the server agrees to access across domains, if we allow any Web site to request resources, you can fill out "*" here, Instead, Access-control-expose-headers can set the return header to pass data, and the headers allowed in a simple request include: Cache-control,content-language, Content-type,expires,last-modified,pragma.

For not so simple as kneeling last year, here is an example:

Options/cors http/1.1origin:http://b.comaccess-control-request-method:putaccess-control-request-headers: x-custom-headerhost:a.comaccept-language:en-usconnection:keep-aliveuser-agent:mozilla/5.0 ...

Where Access-control-request-method represents the type of the real request, while Access-control-request-headers represents the key content of the request header for the real request. The server will not consent to the browser initiating a real request until it verifies the legality of the two contents.

The following is the header setting for the corresponding response:

Access-control-allow-origin:http://b.comaccess-control-allow-methods:get, POST, Putaccess-control-allow-headers: x-custom-headercontent-type:text/html; Charset=utf-8
Another is Access-control-max-age, which is a cache time saved by the browser, in seconds, before the cache expires, the browser does not have to verify that the same type of request is legitimate.



Sinsing Analysis of cors solution for cross-domain transmission

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.