简体   繁体   English

无法使用React.js提取方法连接到API

[英]Can't connect to API using React.js fetch method

I'm trying to connect to buffer's API ( https://buffer.com/developers/api/oauth ) via react.js using fetch(), but I'm getting a 400 error response. 我正在尝试使用fetch()通过react.js连接到缓冲区的API( https://buffer.com/developers/api/oauth ),但是我收到了400错误响应。

This is running on my localhost but the site's accessible from the Internet. 它在我的本地主机上运行,​​但是可以从Internet访问该站点。

Here's my code: 这是我的代码:

const queryString = require('query-string');
        const parsed = queryString.parse(window.location.search);

const buffer_data = {
                client_id: BUFFER_CLIENT_ID,
                client_secret: BUFFER_CLIENT_SECRET,
                redirect_uri: BUFFER_CALLBACK_URL,
                code: parsed.code,
                grant_type: 'authorization_code',
};

fetch(BUFFER_ACCESS_TOKEN_URL, {
                method: 'post',
                body: JSON.stringify(buffer_data),
              }).then( data => {
                console.log('data response ' + data);
                return data.json();
            }).then( response => {
                console.log(response);
});

And here's the response: 这是响应:

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"} {“错误”:“ invalid_request”,“错误描述”:“无效的Grant_type参数或缺少参数”}

And the console prints this: 控制台将输出以下内容:

Failed to load https://api.bufferapp.com/1/oauth2/token.json : No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载https://api.bufferapp.com/1/oauth2/token.json :所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://myserver.com ' is therefore not allowed access. 因此,不允许访问源“ http://myserver.com ”。 The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 该响应的HTTP状态代码为400。如果不透明响应满足您的需求,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。

I've tried lots of things, like not serializing the data, trying sending many different headers, using a CORS chrome plugin, etc 我尝试了很多事情,例如不对数据进行序列化,尝试发送许多不同的标头,使用CORS chrome插件等

Before coding this in react, I've successfully connected using PHP, but to do that I had to add an SSL certificate on my server. 在用React编写代码之前,我已经使用PHP成功连接,但是要做到这一点,我必须在服务器上添加SSL证书。

I'm also open to use any library but haven't found one to do this job. 我也愿意使用任何库,但尚未找到要执行此工作的库。 Or any other fetching method, like axios? 还是其他任何获取方法,例如axios?

Many thanks! 非常感谢!

I don't know which browser you are using, but you might want to add Allow-Control-Allow-Origin 我不知道您使用的是哪种浏览器,但您可能想添加Allow-Control-Allow-Origin

for your chrome browser (if you are using chrome). 适用于您的Chrome浏览器(如果您使用的是chrome)。 This is some issue that seems to occur when you are using localhost with chrome. 当您将localhost与chrome一起使用时,似乎会出现此问题。

Also, i highly recommend using axios for fetching API's, it comes with some easy error logs that can help you pinning down the issue pretty fast. 另外,我强烈建议使用axios来获取API,它带有一些简单的错误日志,可以帮助您快速解决问题。

Greetings! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM