【发布时间】:2018-03-06 02:51:08
【问题描述】:
我正在尝试在 javascript 代码中使用 Watson 文本到语音服务。 但是,我一直在努力让它发挥作用。
如果我使用以下代码:
$.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: 请求头域 Authorization is not allowed by 预检响应中的 Access-Control-Allow-Headers。
请注意,我可以很容易地从 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.
【问题讨论】:
标签: cors basic-authentication restful-authentication ibm-watson watson