zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    Python 列表字典制作名册管理
    AS3获取SWF文件中AS链接
    AS3多选多模型
    AS3视频播放器
    测试
    Makefile三个有用变量$@,$^,$<
    CentOS 7.2 安装教程
    Ubuntu 查看/修改文件编码
    API 进程、线程函数
    API 菜单函数
  • 原文地址:https://www.cnblogs.com/BlingSun/p/9777059.html
Copyright © 2011-2022 走看看