Skip to content

xhr.withCredentials与CORS的关系(跨域 | 认证信息[cookie]) #25

@yongheng2016

Description

@yongheng2016

xhr.withCredentials与CORS的关系

  • 发送同域请求时,浏览器会自动将cookie加在reuqest header中  
  • 发送跨域请求时,浏览器并没有将cookie加在request header中

原因:CORS标准规定,默认情况下,浏览器发送跨域请求时,不能发送任何认证信息(credentials)
如:cookieHTTP authentication schemes

  • [x]解决方法:(下面两个条件必须同时满足)
  1. client端手动设置xhr.withCredentials = true
  2. server端允许requestHeader能携带认证信息,即:Access-Contorl-Allow-Credentials: true
  • 注意:一旦跨域request能够携带认证信息,server端一定不能将Access-Contorl-Allow-Origin设置为*,而必须设置为请求页面的域名

参考:https://segmentfault.com/a/1190000004322487#articleHeader13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions