首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React / jQuery -为什么“Access-Control-Allow-Origin”标头被拒绝?

React / jQuery -为什么“Access-Control-Allow-Origin”标头被拒绝?
EN

Stack Overflow用户
提问于 2017-12-13 13:14:24
回答 1查看 1.4K关注 0票数 0

我正在尽我最大的努力理解为什么在https://www.html5rocks.com/en/tutorials/cors/上遵循CORS指南(他们建议自己遵循)时仍然会出现这个错误

我收到的错误是:

无法加载https://bittrex.com/api/v1.1/public/getmarketsummaries:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源'http://localhost:3000‘。

基于此,我认为问题出在$.ajax调用内部?

代码语言:javascript
运行
复制
import React, { Component } from 'react';
import './App.css';
import $ from 'jquery';

export default class TokenList extends Component {
constructor(props) {
    super(props);

    this.state = {
        object : []
        };
    }

    componentDidMount() {
        this.TokenList();
    }


    TokenList() {

// Method currently not working...

// Method 1
    $.ajax({
         type: 'GET',
         url: 'https://bittrex.com/api/v1.1/public/getmarketsummaries',
         contentType: 'text/plain',
         xhrFields: {
             withCredentials: false
         },

         success: function() {
            console.log('Access Granted!');
         },


// Error suggests the issue is here?
         headers: {
            'Access-Control-Allow-Origin' : 'http://localhost:3000'
         },

         error: function() {
             alert('Error making the request');
         }

     })
     }
EN

回答 1

Stack Overflow用户

发布于 2017-12-13 16:33:40

CORS有很多混淆之处

但在底线上,您没有在客户端请求上设置CORS。

后端假定在响应中返回正确的标头。

像这样:

代码语言:javascript
运行
复制
 headers: {
        'Access-Control-Allow-Origin' : 'http://localhost:3000'
     },

这应该来自后端,而不是你在客户端设置它。

如果您没有控制后端,则需要检查该提供商发送的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47785929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档