我在html中有这个表单,它同时包含文件和其他输入元素:
<form action="/imsick/disease/add" id="addDisease" method="post" role="form" data-async="" enctype="multipart/form-data">
<input type="text" id="name" name="name" class="form-control">
<textarea id="description" name="description" class="form-control" rows="10"></textarea>
<input type="checkbox" name="category[]" value="3">
<input type="checkbox" name="category[]" value="5">
<input type="checkbox" name="category[]" value="6">
<input type="checkbox" name="category[]" value="4">
<input type="checkbox" name="category[]" value="1">
<input type="checkbox" name="category[]" value="7">
<input type="checkbox" name="category[]" value="2">
<input type="file" id="approach-file" name="approach">
<input type="checkbox" name="symptom[0][is_patogonomic]">
<input type="text" name="symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">
<input type="checkbox" name="symptom[1][is_patogonomic]">
<input type="text" name="symptom[1][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">
<input type="text" name="lab_symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/labsymptom/search">
<select name="lab_symptom[0][anomaly_type]" class="form-control">
<option value="ABOVE">ABOVE</option>
<option value="UNDER">UNDER</option>
<option value="POSITIVE">POSITIVE</option>
<option value="NEGATIVE">NEGATIVE</option>
<option value="++">++</option>
</select>
<input type="submit" class="btn btn-default center-block">
</form>
这是我的javascript / jquery代码,用于通过ajax提交表单数据:
$('form[data-async]').on('submit', '', function(event) {
//form data object
event.preventDefault();
var $form = $(this);
var __fd = new FormData($form[0]);
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: __fd,
processData: false,
beforeSend: function(xhr) {
//not important
},
success: function(data, status) {
data = $.parseJSON(data);
if (data.success) {
//not important
} else {
//not important
}
},
error: function() {
//not important
}
});
return false;
});
提交数据后,在表单的目标网址上执行var_dump($_POST)
,会得到以下信息:
array(2) {
["_url"]=>
string(12) "/disease/add"
["------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition:_form-data;_name"]=>
string(1404) ""name"
asdasdaasdasdasdasda
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="description"
asdsad
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
5
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
4
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
1
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="category[]"
7
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="approach"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[0][is_patogonomic]"
on
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[0][name]"
asdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[1][is_patogonomic]"
on
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="symptom[1][name]"
asdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="lab_symptom[0][name]"
asdasdasd
------WebKitFormBoundaryHmT1pwGJDYbVxvrN
Content-Disposition: form-data; name="lab_symptom[0][anomaly_type]"
ABOVE
------WebKitFormBoundaryHmT1pwGJDYbVxvrN--
"
}
似乎php不解析这些数据,$ _POST数组只有两个成员。
谁能告诉我我做错了什么?
感谢
答案 0 :(得分:4)
尝试添加contentType: false
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: __fd,
processData: false,
contentType: false , // tell jQuery not to set contentType
beforeSend: function(xhr) {
//not important
},
success: function(data, status) {
data = $.parseJSON(data);
if (data.success) {
//not important
} else {
//not important
}
},
error: function() {
//not important
}
});