我正在尝试将图像发送到名为 Cloudsight 的图像识别 API。我已经使用 URL 让它工作,现在正试图让它发送本地图像。问题是,它不需要任何用户交互,因为这一切都必须自动发生。我看到的答案使用 FormData,但由于它不需要用户交互,我认为我不能使用它。(除非有办法使用它来自动上传东西。)目前的代码是:
var token; //Variable for use later
//First AJAX request.
$.ajax({
method: "POST",
url: "https://api.cloudsightapi.com/image_requests",
beforeSend: function(xhr) { // Authorizes the request.
xhr.setRequestHeader("Authorization",
"CloudSight [key]");
},
data: { // The data to send.
"image_request[image]": //the image needs to go here,
"image_request[locale]": "en-US"
},
success: function(msg) { // What should happen if succesful.
console.log("It worked! :D Good POST request.");
console.log(msg);
token = msg.token; // Assigns the token the POST request returns to the token variable.
get(); // Calls the function containing the GET request.
},
error: function(msg) { // What should happen if not succesful.
console.log("Sorry...");
console.log(msg);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我所需要的只是发送本地文件;如何在没有用户交互的情况下上传图像?