我需要在提交事件时检查一个文件,所以我做了以下操作:
$(document).ready(function(){
$("form").submit(function(){
data = new FormData();
data.append('picture', $('#picture-field')[0].files[0]);
$.ajax({
url: 'checkit.php',
data: data,
processData: false,
cache: false,
contentType: false,
type: 'POST',
dataType: "json",
success: function (data) {
alert(data.error)
}
});
});
});
但这行不通。返回的数据没有被显示,有人能帮我吗?
尝试阻止表单提交操作
$(document).ready(function () {
$("form").submit(function (e) {
//stop the default form submit action
e.preventDefault();
var files = $('#picture-field')[0].files;
if (!files.length) {
alert('select a file')
return;
}
var data = new FormData();
data.append('picture', $('#picture-field')[0].files[0]);
$.ajax({
url: 'checkit.php',
data: data,
processData: false,
cache: false,
contentType: false,
type: 'POST',
dataType: "json",
success: function (data) {
alert(data.error)
},
error: function (jqXhr, status, error) {
alert(status + ':' + error + ':' + jqXhr.responseText)
}
});
});
});