我想使用沃森文本到語音服務的JavaScript代碼。 但是,我試圖讓它工作。授權問題沃森文本到語音轉換從Java腳本
如果我使用下面的代碼:
$.ajax({
url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize',
type: 'POST',
headers: {"Content-Type": "application/json", "Accept": "audio/*", "Authorization": "Basic SomethingSomethingSomething=="},
text: msgData.message[0].cInfo.text,
output: 'output.wav',
success: function() {
console.log("text to voice complete");
var audio = new Audio('output.wav');
audio.play();
}
});
我得到:
無法加載 https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize: 請求頭字段授權不被 訪問控制允許報頭允許在飛行前響應。
請注意,我可以很容易地得到這樣的要求是從Restlet工作。
不過,如果我使用:
$.ajax({
url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize',
type: 'POST',
user: {"something": "something"},
headers: {"Content-Type": "application/json", "Accept": "audio/*"},
data: {"text": msgData.message[0].cInfo.body},
output: 'output.wav',
success: function() {
console.log("text to voice complete");
var audio = new Audio('output.wav');
audio.play();
}
});
我得到:
stream.watsonplatform.net/text-to-speech/api/v1/synthesize:1 POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize 401 (Processed)
index.html:1 Failed to load https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://52.207.232.200' is therefore not allowed access. The response had HTTP status code 401.
見https://stackoverflow.com/questions/43105146/how-to-call-ibm-watson-services-from-javascript/43106268#43106268 – sideshowbarker