【发布时间】:2016-09-07 06:16:06
【问题描述】:
我可以使用下面的表格获取 json 数据:
<form enctype="multipart/form-data" action="http://46.51.220.101/kookooapi/index.php/IVRSUploadFile/UploadFile" method="post">
<input id="default_file" name="file" type="file" />
<input id="account" name="account" type="hidden" value="testing" />
<input type="submit" value="Save">
</form>
但与 jquery ajax 一起使用时相同的表单会引发错误:501 (Not Implemented)
$('#default_file').change(function (e) {
//on change event
formdata = new FormData();
if ($(this).prop('files').length > 0)
{
file = $(this).prop('files')[0];
formdata.append("music", file);
}
});
function hitexternal(e) {
$.ajax({
url: "http://46.51.220.101/kookooapi/index.php/IVRSUploadFile/UploadFile",
type: "POST",
data: new FormData($("#default_file")[0], {data :{"account":"testing"}}),
processData: false,
contentType: 'application/json',
success: function (response) {
console.log('resp: ' , response);
},
error: function (jqXHR, textStatus, errorMessage) {
console.log("error:" , errorMessage);
}
});
e.preventDefault();
}
我错过了什么?
【问题讨论】:
-
我认为你错过了“如何构造 FormData” - developer.mozilla.org/en-US/docs/Web/API/FormData/FormData - 另外,你的
change函数修改了一些全局变量......似乎没有被使用,所以它基本上没有意义就显示的代码而言
标签: javascript jquery ajax