Ajax跨域(CROS)请求中的Preflighted Requests

预检请求(Preflighted requests )是一种浏览器跨域请求中询问服务器对跨域请求的支持情况。

关于预检请求

满足预检请求的2个重要特征(满足其中之一):

  1. 使用了 GET, HEAD , POST,PUT等方法时,Content-Type传输类型是 application/x-www-form-urlencodedmultipart/form-datatext/plain,text/xml等

  2. 请求头使用了自定义的Header

举个例子

 

var xhr= new XMLHttpRequest();
var url = 'http://bar.other/resources/post-here/';
var body = '<?xml version="1.0"?><person><name>Arun</name></person>';
function callOtherDomain(){
  if(invocation)
    {
      xhr.open('POST', url, true);
      xhr.setRequestHeader('X-PINGOTHER', 'pingpong'); //自定义的Header
      xhr.setRequestHeader('Content-Type', 'application/xml'); //特殊的文档类型
      xhr.onreadystatechange = function(){};
      xhr.send(body); 
}
}

 

满足以上之一,便可发起预检请求,预检请求流程如下

OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type


HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 0
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/plain

POST /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-PINGOTHER: pingpong
Content-Type: text/xml; charset=UTF-8
Referer: http://foo.example/examples/preflightInvocation.html
Content-Length: 55
Origin: http://foo.example
Pragma: no-cache
Cache-Control: no-cache

<?xml version="1.0"?><person><name>Arun</name></person>


HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:40 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 235
Keep-Alive: timeout=2, max=99
Connection: Keep-Alive
Content-Type: text/plain

[Some GZIP'd payload]

 

我们看到,首次发起的是OPTIONS请求,因为OPTIONS请求的作用本身就是询问服务器的请求,他这里询问浏览器是否支持如下条件请求

Access-Control-Request-Method: POST  
#是否支持POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type  
#是否支持X-PINGOTHER自定义请求头的内容,是否支持Content-Type指定的类型

 

服务器回应是

Access-Control-Allow-Origin: http://foo.example
#支持foo.example域
Access-Control-Allow-Methods: POST, GET, OPTIONS
#支持的方法POST,GET,OPTIONS,实际上OPTIONS本身就支持
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
#支持的请求头
Access-Control-Max-Age: 86400
#支持的缓存时间

 

如果满足以上条件,浏览器会自动发起提交之前没提交的数据,否则拒绝提交数据。

补充一下withCredentials的作用

在跨域请求中,类似Cookie等敏感信息一般不会跨域传输,但是在服务器允许的情况下,Cookie会被发送

当然,服务器要允许

Access-Control-Allow-Credentials: true

浏览器也要允许

var xhr= new XMLHttpRequest();

var url = 'http://bar.other/resources/post-here/';
var body = '<?xml version="1.0"?><person><name>Arun</name></person>';

function callOtherDomain(){
  if(invocation)
    {
      xhr.withCredentials = true;#允许cookie信息
      xhr.open('POST', url, true);
      xhr.setRequestHeader('X-PINGOTHER', 'pingpong'); //自定义的Header
      xhr.setRequestHeader('Content-Type', 'application/xml'); //特殊的文档类型
      xhr.onreadystatechange = function(){};
      xhr.send(body); 
}
}

 

服务器设计建议

服务器设计,我们不仅要处理常见的跨域请求,对于Preflighted  Request请求,我们更应该保证浏览器能得到预检请求的结果,因此,服务器端一定要处理OPTIONS请求

 

你可能感兴趣的:(options)