fetch跨域请求 + spring mv 跨域支持

JS发起请求:
fetch(url, {
  method: 'POST',
  body: 'p1=v1&p2=v2',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  credentials: 'same-origin',
  mode: 'cors'
})
    credentials

 
 
  • omit: 默认值,忽略cookie的发送

  • same-origin: 表示cookie只能同域发送,不能跨域发送

  • include: cookie既可以同域发送,也可以跨域发送

设置include后, 服务端 response header 中 Access-Control-Allow-Origin 必须设置为request的Origin。否则报错:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'null' is therefore not allowed access.
springMVC
V.4.3+ 直接使用注解 @CrossOrigin
参考: http://www.cnblogs.com/cielosun/p/6741307.html
V.4.3- 设置response的header
     response.addHeader("Access-Control-Allow-Origin","*");
     response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
     response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Access-Control-Max-Age", "1800");//30 min



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值