我正在尝试使用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(坏请求)我做错了什么?
你试过这样的方法吗?
beforeSend: function (req){
req.setRequestHeader("Authorization", "CloudSight [key]");
},
如果有人看到这个,设法用这段代码解决问题:
$.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对授权的帮助!