【发布时间】:2016-08-31 16:05:24
【问题描述】:
所以我正在努力将视频发布到Emotion API for video,但我一直无法得到回复。
我已经能够让它在 Microsoft online console 上运行,但是当我尝试使用 (1) JavaScript Ajax 或 (2) Ruby 服务器端代码在我的 Rails 应用程序中实现它时,我始终如一得到各种错误。
这是我的代码。起初我尝试使用 Ajax 方式,但我怀疑 API 没有启用 CORS。所以后来我尝试了Ruby,没有成功。
Ruby 尝试:
def index
uri = URI('https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo')
uri.query = URI.encode_www_form({
})
data = File.read("./public/mark_zuck.mov")
request = Net::HTTP::Post.new(uri.request_uri)
# Request headers
request['Ocp-Apim-Subscription-Key'] = 'e0ae8aad4c7f4e33b51d776730cff5a9'
# Request body
request.body = data
request.content_type = "video/mov"
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
puts response.body
end
这是我的 Ajax 尝试:
function CallAPI(apiUrl, apiKey){
console.log("API called");
$(".loading").css("display", "inline-block");
$.ajax({
url: apiUrl,
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
},
type: "POST",
data: '{"url": "http://localhost:5000/mark_zuck.mov"}',
processData: false,
success: function(response){
console.log("API success");
ProcessResult(response);
$(".loading").css("display", "none");
console.log(response);
},
error: function(error){
console.log("API failed");
$("#response").text(error.getAllResponseHeaders());
$(".loading").css("display", "none");
console.log(error);
}
})
是的,我已经重新生成了我的密钥。这只是为了说明我的观点。
【问题讨论】:
-
“我经常遇到各种错误” - 定义“错误”是什么意思?
-
我的 ajax 尝试产生了 400 错误以及“Access-Control-Allow-Origin not set”错误。我的红宝石尝试产生
{"error":{"code":"BadArgument","message":"Invalid Media Type."}} -
您确定您正确实现了 API 客户端吗? CORS 通常会阻止网站的代码在其他地方工作。如果没有可用的 api,则可以使用 Selenium
标签: javascript ruby ajax api emotion