[英]Sending binary data in ajax/jquery REST api
To everyone who mask this as a duplicate; 对于将此作为副本掩盖的每个人; please see the other answers you are refering to and the links I have provided here.
请参阅您提及的其他答案以及我在此处提供的链接。 They content type is
audio/wav
and not stream/octet
as mentioned in other answers. 它们的内容类型是
audio/wav
而不是其他答案中提到的stream/octet
。 Please don't mark questions as duplicates without reading its content 请勿在不阅读其内容的情况下将问题标记为重复
I am trying to send an audio file to a server, so I can get an JSON response back. 我正在尝试将音频文件发送到服务器,因此我可以获得JSON响应。 This is an IBM Service so we have the REST API provided by them.
这是一个IBM服务,因此我们提供了他们提供的REST API。 Below is my code in
ajax
下面是我在
ajax
代码
function recognize()
{
$.ajax
({
type: "POST",
url: "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize",
dataType: 'json',
username: "xxxx",
password: "xxxx",
contentType: "audio/wav",
success: function (data){
alert(JSON.stringify(data));
}
});
}
Below is the IBM example for the REST call I am going to make - 下面是我将要进行的REST调用的IBM示例 -
curl -u "{username}":"{password}" \
-H "content-type: audio/wav" \
--data-binary @"/path/to/file.wav" \
"https://stream.watsonplatform.net/speech-to-text/api/v1/sessions/{session_id}/recognize"
The link to the example page and description is - http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text/api/v1/#recognize 示例页面和说明的链接是 - http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/speech-to-text/api/v1/#recognize
My question is, how Can I send the binary file via Ajax? 我的问题是,如何通过Ajax发送二进制文件? This is actually a phonegap application.
这实际上是一个phonegap应用程序。
Here is an interesting question; 这是一个有趣的问题; if I send the URI of the file into
data:
tag of the REST call it works. 如果我将文件的URI发送到
data:
REST调用的标签,它的工作原理。 If I send the real path it do not. 如果我发送真实路径它不会。 why???
为什么???
You can use XMLHttpRequest to send binary data 您可以使用XMLHttpRequest发送二进制数据
ar oReq = new XMLHttpRequest();
oReq.open("POST", url, true);
oReq.onload = function (oEvent) {
// Uploaded.
};
var blob = new Blob(['abc123'], {type: 'audio/wav'});
oReq.send(blob);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.