码迷,mamicode.com
首页 > 其他好文 > 详细

fetch跨域问题

时间:2018-10-12 11:54:32      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:origin   -o   include   url   failed   headers   cti   mode   服务器端   

 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.

 

fetch跨域问题

标签:origin   -o   include   url   failed   headers   cti   mode   服务器端   

原文地址:https://www.cnblogs.com/BlingSun/p/9777059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!