IBM Watson Conversation API:“在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段授权” [英] IBM Watson Conversation API: “Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response”

查看:183
本文介绍了IBM Watson Conversation API:“在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段授权”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个与Watson REST API连接的React-Native应用程序。使用作为ReactNative一部分的fetch库,一切都可以很好地获取Workspaces列表,如下所示:

I created a React-Native app that connected to the Watson REST APIs. Using the fetch library that is part of the ReactNative, everything was working well for getting the Workspaces list, like this:

    const myAuth = new Buffer(USR+':'+PWD).toString('base64');
    const myInit = {
        method: 'GET',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Origin': '',
            'Authorization': 'Basic ' + myAuth,
        }
    };

    return fetch(URL, myInit)
        .then((response) => response.json())
        .then((responseJson) => { ... }

我现在正在使用React(而不是ReactNative)和 whatwg-fetch 库。相同的代码不起作用。首先我得到了以下错误:

I am now moving to React (rather than ReactNative) and the whatwg-fetch library. The same code does not work. First I got the error that:


请求标头字段在飞行前响应中,Access-Control-Allow-Headers不允许授权。

Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

然后阅读有关此类问题的数十个线程,这简直是不可思议的事情,为Access-Control-Allow-Methods,Access-Control-Allow-Headers等添加标题。最终,问题变成了:

Then reading the tens of threads about these type of issues it just became black magic. Adding headers for Access-Control-Allow-Methods, Access-Control-Allow-Headers, and the such. Nothing really worked so far. Eventually the problem became:


请求标头字段access-control-allow-headers在Access-Control-Allow-Headers中是不允许的飞行前响应。

Request header field access-control-allow-headers is not allowed by Access-Control-Allow-Headers in preflight response.

有人可以给我指出一个示例或可以工作的代码吗?

Can someone point me please to an example or code that works please?

=============

==============

更新...

感谢@sideshowbarker和@FakeRainBrigand。我猜想浏览器客户端应用程序必须具有服务器端。

Thank you @sideshowbarker and @FakeRainBrigand. I guess then a server side is a must for the browser client app.

推荐答案

浏览器拒绝发送您的请求,因为您将请求发送到的服务器尚未启用CORS,至少没有启用这些标头。

The browser is refusing to send your request because the server you're sending the request to hasn't enabled CORS, at least not with those headers.

唯一无需修改目标服务器的解决方案是编写您自己的服务器,代表客户端进行请求。

The only solution without modifying the server you're targeting is to write your own server that makes the request on behalf of the client.

这篇关于IBM Watson Conversation API:“在飞行前响应中,Access-Control-Allow-Headers不允许请求标头字段授权”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆