我做了很多测试,通过jQuery ajax上传文件,但我仍然有这个错误: 400错误请求。
这是我的代码:
function createDatasetSync() {
var form = $('#fileUploadForm')[0];
var data = new FormData(form);
$.ajax({
type: 'POST',
url: '<remote API to call>',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + <myToken>);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
},
data: data,
cache: false,
contentType: false,
processData: false,
timeout: 600000,
success: function (data) {
console.log(data);
},
error: function (data) {
console.log("ERROR: " , data);
}
});
}
$( document ).ready(function() {
$("#btnSubmit").click(function (event) {
event.preventDefault();
createDatasetSync();
});
});
&#13;
<html>
<body>
<form method="POST" enctype="multipart/form-data" id="fileUploadForm">
<input type="file" name="files"/><br/><br/>
<input type="submit" value="Submit" id="btnSubmit"/>
</form>
</body>
</html>
&#13;
我必须将.zip文件发送到远程Web服务。
这是我试图复制的cURL电话:
curl -X POST -H "Authorization: Bearer myTOKEN" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "data=@C:\myFile.zip" -F "type=image" URL_to_call
提前致谢。
答案 0 :(得分:0)
$("#btnSubmit").click(function (event) {
event.preventDefault();
createDatasetSync();
});
此代码不适用于提交按钮。将按钮类型属性更改为submit