fetch跨域问题

 fetch(url,{
        method:'post', 
        mode:"cors",      //允许跨域  no-cors不允许跨域
      //  credentials:"include",  //跨域请求时是不带cookie的,添加该属性表示强制加入凭据头,请求时就会携带cookie。但是如果加上这个属性,那么服务器的Access-Control-Allow-Origin 就不能是‘*’,否则会报下面的错误。
        headers:new Headers({
         'Content-Type': 'application/x-www-form-urlencoded', // 指定提交方式为表单提交
          }),
          body:formdate
        }) .then(function(response) {
    
    return response.json();

  }).then(function(json) {
    console.log('parsed json', json);
    
  }).catch(function(ex) {
    console.log('parsing failed', ex);
   
  })

跨域设置
credentials:"include"时,如果服务器端设置Access-Control-Allow-Origin 为‘*’会报如下错误。可以去掉该请求头的设置,只添加mode:'cors'属性。
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 'http://localhost:8080' is therefore not allowed access.

 

转载于:https://www.cnblogs.com/BlingSun/p/9777059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值