簡體   English   中英

AngularJs和Bluemix QA API無法發出發布請求

[英]AngularJs and Bluemix QA API can't make a post request

我正在嘗試將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.

如果我嘗試刪除標頭,則會在另一個標頭中給我一個問題。 那么,有什么想法嗎? 還是任何ajax示例? 我知道有一些nodejs示例,但是我想知道是否可以直接連接到api。

之所以得到這個,是因為您試圖執行跨站點腳本。 Watson不支持此功能。 您將需要將該請求代理到您的應用程序的后端,然后讓您的應用程序將請求漏斗到Watson。

例如,這里有一個與Personality Insights一起使用的應用程序 ,它將請求從Angular到后端再到Watson進行了傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM