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.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM