我正在尽我最大的努力理解为什么在https://www.html5rocks.com/en/tutorials/cors/上遵循CORS指南(他们建议自己遵循)时仍然会出现这个错误
我收到的错误是:
无法加载https://bittrex.com/api/v1.1/public/getmarketsummaries:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源'http://localhost:3000‘。
基于此,我认为问题出在$.ajax调用内部?
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');
}
})
}发布于 2017-12-13 16:33:40
CORS有很多混淆之处
但在底线上,您没有在客户端请求上设置CORS。
后端假定在响应中返回正确的标头。
像这样:
headers: {
'Access-Control-Allow-Origin' : 'http://localhost:3000'
},这应该来自后端,而不是你在客户端设置它。
如果您没有控制后端,则需要检查该提供商发送的内容。
https://stackoverflow.com/questions/47785929
复制相似问题