`

Js设置前端允许跨域请求后端API:Access-Control-Allow-Credentials

    博客分类:
  • js
阅读更多

跨域报错信息:

Fetch API cannot load https://xxx.com/api. Response to preflight 
request doesn't pass access control check: 
The value of the 'Access-Control-Allow-Credentials' header 
in the response is '' which must be 'true' 
when the request's credentials mode is 'include'. 
Origin 'https://xxx.com' is therefore not allowed access.

 

Method1:纯js

export function Cors(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.withCredentials = true; // 设置运行跨域操作
    xhr.send();
    xhr.onload = () => {
      const text = xhr.responseText;
      if (xhr.status === 200) {
        resolve(JSON.parse(text));
      } else {
        let error = new Error(text);
        error.response = xhr;
        reject(error);
      }
    };
  });
}

 

Methods2:jQuery版

$.ajax({
  url : 'xxx',
  xhrFields: {
    withCredentials: true // 设置运行跨域操作
  },
  success : function(data) {
    console.log(data);
  }
});

 

 

Methods3:fetch(部分浏览器不兼容,不建议使用)

export function Get(url) {
  const config = {
    method: 'get',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    credentials: 'include'
  };
  return fetch(url, config)
    .then(checkStatus)
    .then(parseJSON);
}

 

Methods4: axios.js跨域:

axios(url, {
  method: 'GET',
  mode: 'no-cors',
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
  },
  withCredentials: true,
  credentials: 'same-origin',
}).then(response => {
})

 

更多实例应用扫码体验:

分享到:
评论
1 楼 自己811005 2018-01-14  
88350bcf69dcfbda7f8a76a589d90543

相关推荐

    Nginx跨域设置Access-Control-Allow-Origin无效的解决办法

    add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET,POST'; 使用以下配置,生效。 if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-...

    neo-cors:在 Neo 应用程序中启用 CORS

    新科尔斯 在应用程序中启用 CORS ...Access-Control-Allow-Credentials: "true" Access-Control-Allow-Methods: "*" Access-Control-Allow-Headers: "*" Access-Control-Max-Age: 600 Access-Control-Expose-Headers:

    cros-anywhere-crx插件

    当Access-Control-Allow-Credentials 为 true 时(跨域传递cookie), Access-Control-Allow-Origin需要显式指定来源域名, 而不是之前的*When Access-Control-Allow-Credentials is true ( need transfer cookie for ...

    Django配置跨域并开发测试接口

    1.跨域原理 1. 首先浏览器安全策略限制js ajax跨域...Access-Control-Allow-Credentials: true 那么, 浏览器可以让js 请求该服务器 2.django cors设置: 1. 安装包 pip install django-cors-headers 2. 注册应用 INS

    Vue 项目中遇到的跨域问题及解决方法(后台php)

    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’. The value of the ‘Access-Control-Allow-...

    Moesif Origin & CORS Changer-crx插件

    您可以覆盖:请求标头:源头响应标头:Access-Control-Allow-Origin,Access-Control-Allow-方法,Access-Control-Allow-Header,Access-Control-Allow-Credentials,Access-Control-Expose-Header CORS指南:...

    深入浅析Nginx实现AJAX跨域请求问题

    AJAX从一个域请求另一个域会有跨域的问题。那么如何在nginx上实现ajax跨域请求呢?...add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET'; ... ... the re

    EurekaLog_7.5.0.0_Enterprise

    1)....Added: HKCU\Software\EurekaLab\Viewer\4.0\UI\Statuses registry key to allow status customizations in Viewer 2)....Added: "Disable hang detection under debugger" option 3)....Fixed: Wrong button ...

    Nginx服务器中处理AJAX跨域请求的配置方法讲解

    Nginx 实现AJAX跨域请求 AJAX从一个域请求另一个域会有跨域的问题。那么如何在nginx上实现ajax跨域请求呢?...add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET

    axios中cookie跨域及相关配置示例详解

    前言 最近在跨域、cookie 以及表单上传这几个方面遇到了点小问题,做个简单探究和总结。... header信息 Access-Control-Allow-Credentials:true Access-Control-Allow-Origin不可以为 ‘*’,因为 ‘*’ 会和 Access

    Node.js设置CORS跨域请求中多域名白名单的方法

    res.header('Access-Control-Allow-Credentials', 'true'); // 允许服务器端发送Cookie数据 然而,这样的设置是最简单粗暴,同时也是最不安全的。它表示该接口允许所有的域名对它进行跨域请求。然而,在一般实际

    event-logger-dapp:示例DAPP旨在利用以太坊智能合约和IPFS在区块链上记录事件并将JSON文件存储到IPFS

    事件记录器-dapp 示例DAPP旨在利用以太坊... 要运行此应用程序,必须安装正在运行的程序: IPFS- //ipfs.io/ geth- //geth.ethereum.org/downloads/ 在启动ipfs之前,请确保将CORS标头设置为: ipfs config --json API....

    Java开发基于区块链的去中心化拍卖系统源码+项目说明.zip

    "Access-Control-Allow-Credentials": ["true"], "Access-Control-Allow-Headers": ["Authorization"], "Access-Control-Allow-Methods": ["GET","POST"], "Access-Control-Allow-Origin": ["*"], ...

    chrome-cross-domain:Chrome扩展跨域

    跨域,跨域,自动添加Access-Control-Allow-Origin标头显示盗链图片,删除Referer标头特征: 添加Access-Control-Allow-Origin标头支持跨cookie,跨域支持cookie(添加Access-Control-Allow-Credentials标头) 删除...

    详解Vue 开发模式下跨域问题

    后端设置请求头部Access-Control-Allow-Credentials: true和Access-Control-Allow-Origin: www.xxx.com 前端post请求设置withCredentials=true 这里用了axios的请求数据方法代码如下: import axios from 'axios...

    MyServerDashboard:具有本地缓存​​的聚合统计信息

    我的服务器仪表盘 具有本地缓存​​的聚合统计信息 ... add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS'; add_header 'Access-Control

    xj_20210207_ibas0742

    新疆项目(重建)配置url配置项在启动项目需要启动的服务有1, D:\... add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Acce

    微信小程序 +nodejs+socket.io bug

    问题描述:技术 nodejs socket.io 微信小程序   源码 server.js   ...app.use(function(req, res, next) {  res.setHeader('Access... res.setHeader('Access-Control-Allow-Credentials', true)  res.setHeader('Acc

    乐优商城.xmind

    Access-Control-Allow-Credentials:是否允许携带cookie,默认情况下,cors不会携带cookie,除非这个值是true 实现非常简单 gateway网关中编写一个配置类 GlobalCorsConfig 添加CORS配置信息 允许的域,不要写*...

Global site tag (gtag.js) - Google Analytics