Jquery Ajax API错误:400错误请求

时间:2015-10-19 16:20:29

标签: javascript jquery ajax api cloudsight

我正在尝试使用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(错误请求)我做错了什么?据我所知,代码似乎没问题......

2 个答案:

答案 0 :(得分:1)

你尝试过这样的事吗?

beforeSend: function (req){
    req.setRequestHeader("Authorization", "CloudSight [key]");
},

答案 1 :(得分:0)

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

&#13;
&#13;
$.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);
  }
});
&#13;
&#13;
&#13;

感谢Mario Cezar对授权的帮助!