0

我正在尝试使用 Jquery 向名为 Cloudsight 的图像识别 API 发出 Ajax POST 请求。到目前为止,我的代码是:

$.ajax({
  type: "POST",
  url: "http://api.cloudsightapi.com/image_requests",
  Authorization: "CloudSight [key]",
  data: {
    remote_image_url: "https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
    locale: "en-US"
  },
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

当我尝试运行它时,我得到错误:400(错误请求)我做错了什么?据我所见,代码似乎没问题...

4

2 回答 2

1

你有没有尝试过这样的事情?

beforeSend: function (req){
    req.setRequestHeader("Authorization", "CloudSight [key]");
},
于 2015-10-19T16:33:32.087 回答
0

万一其他人看到这个,设法用这段代码解决问题:

$.ajax({
  method: "POST",
  url: "https://api.cloudsightapi.com/image_requests",
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Authorization", "CloudSight [key]");
  },
  data: {
    "image_request[remote_image_url]": "https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
    "image_request[locale]": "en-US"
  },
  success: function(msg) {
    console.log("It worked! :D Good POST request.");
    console.log(msg);
    console.log(msg.token);
    console.log(msg.url);
    console.log(msg.responseText);

    token = msg.token;
  },
  error: function(msg) {
    console.log("Sorry...");
    console.log(msg);
    console.log(msg.responseText);
  }
});

感谢 Mario Cezar 对授权的帮助!

于 2015-10-20T16:13:10.867 回答