我正在尝试将我的 Angular 应用程序连接到 Bluemix QA API。所以我有这个代码。
$http({
method: 'POST',
url: 'https://gateway.watsonplatform.net/question-and-answer-beta/api/v1/question/healthcare',
headers: {
'Content-Type': 'application/json',
'Authorization':'Basic mytoken',
'X-SyncTimeout': 30
},
data: {
'question': {
'questionText': 'Malaria?'
}
}
}).then(function(response){
$scope.response = JSON.stringify(response);
});
我的 app.js 上也有这个
.config([
'$routeProvider',
'$httpProvider',
function($routeProvider, $httpProvider){
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
$httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = '*';
}])
但是当我尝试运行该方法时出现此错误:
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers.
如果我尝试删除 header ,则会在另一个 header 中给我带来问题。那么,有什么想法吗?或者有ajax的例子吗?我知道有一些 Nodejs 示例,但我想知道是否可以直接连接到 api。
最佳答案
您收到此消息是因为您正在尝试执行跨站点脚本编写。沃森不支持这一点。您需要将请求代理到应用程序的后端,然后让您的应用程序将请求传送到 Watson。
例如这里是 an app与 Personality Insights 配合使用,将请求从 Angular 传送到后端,然后传送到 Watson。
关于javascript - AngularJs 和 Bluemix QA API 无法发出发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186833/