未經授權點擊該按鈕時,我收到此錯誤響應:POST呼叫bluemix對話框
「POST https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation 401(未授權)」
這裏是我的代碼:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<button onclick="makePostCall()">Click me</button>
<script>
makePostCall = function() {
var username = "c0ae64ef-410a-4f74-b875-ef52dee90686";
var password = "9K2q4byngzo7";
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://watson-api-explorer.mybluemix.net/dialog/api/v1/dialogs/c8e08780-b08b-4cdb-8b8c-ea118863fcd1/conversation', true);
//xhr.withCredentials = true;
xhr.setRequestHeader('Access-Control-Allow-Origin', 'http://localhost:80');
xhr.setRequestHeader('Access-Control-Allow-Credentials', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type', 'application/json', 'Authorization');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + " " + password));
xhr.send('{"query":{"match_all":{}}}');
}
</script>
</body>
</html>
你需要做的第一個授權該請求,以便發送用戶名和密碼在頭上它可能適合你 –