我试图通过我的AJAX请求向我的PHP脚本发送一个图像文件和一个数组,但是数组或图像文件都不显示。我已经找到了问题是行,当你处理文件时,你必须添加到你的AJAX。
contentType:false,processData:false,
如果添加这两行,数组将到达PHP脚本empty。我能做些什么不同的事?:)
我的AJAX:
$('#createMember').on('submit', function(event){
event.preventDefault();
$("#LoadingIcon").show();
var form_data = $(this).serialize();
$.ajax({
url:"scripts/createMember.php",
method:"POST",
data:form_data,
cache: false,
contentType: false,
processData: false,
success:function(data)
{
$.notify({
title: '<strong>Created!</strong><br>',
message: 'Member is now created!'
});
$("#LoadingIcon").fadeOut(200);
});
});
我的HTML
<form method="post" id="opretMedlem" enctype="multipart/form-data">
<input type="text" name="info[name]" class="form-control1 info" />
<input type="text" name="info[age]" class="form-control1 info" />
<input type="file" name="info[image]" class="form-control1 info" />
<input type="submit" name="submit" class="btn btn-info" value="Create member" />
</form>
您忘记在ajax请求中使用enctype。还可以尝试使用FormData()构造函数,而不是序列化数据。
$('#opretMedlem').on('submit', function(event){
event.preventDefault();
var form_data = $(this).serialize();
var form = $(this)[0];
var data = new FormData(form);
$.ajax({
url:"scripts/createMember.php",
enctype: 'multipart/form-data',
method:"POST",
data:data,
cache: false,
contentType: false,
processData: false,
success:function(data) {
alert('done');
}
});
});
看小提琴:https://jsfiddle.net/up8zavtb/