带图像Javascript的HTTP Post请求

时间:2015-10-14 12:16:24

标签: javascript api http post cloudsight

我正在尝试使用Javascript向图像识别API提交帖子请求,但我无法弄清楚如何发送实际图像。我知道我不能仅使用本地文件发送它(当我尝试运行它时,我得到“仅支持协议方案的交叉源请求”错误)。如何使用Post请求发送图像?

到目前为止我的代码是:

var xhr = new XMLHttpRequest();

xhr.open("POST", "cigarette.jpg", false);
xhr.setRequestHeader("Authorization", "CloudSight [key]");

xhr.send("http://api.cloudsightapi.com/image_requests");

console.log(xhr.status);
console.log(xhr.statusText);

我对Javascript和API很陌生,之前并没有真正做过这样的事情。如何在不进入令人难以置信的复杂事物的情况下发送图像?

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

  1. 您需要正确的HTTP标头才能移植图片。
  2. 您无法像这样张贴JPG图片。
  3. 此外,您没有正确使用API​​,请在此处查看如何操作。

    https://cloudsight.readme.io/docs/testinput

    还可以在curl中找到示例

    curl -i -X POST \
    -H "Authorization: CloudSight [key]" \
    -F "image_request[image]=@Image.jpg" \
    -F "image_request[locale]=en-US" \
    https://api.cloudsightapi.com/image_requests