我已在其他帖子中阅读了SO,类似于我发生的事情,但我仍然无法得到解决方案
$('#edit-continue').on('click', function(e) {
e.preventDefault();
var photo = new FormData(); <-----
jQuery.each(jQuery('#photo')[0].files, function(i, file) {<----- SO suggest for file upload
photo.append('file-' + i, file); <-----
});
$.ajax({
type: "POST",
url: "/templates/staycation/common/edit-profile.php",
data: {
id: $('#id').val(),
email: $('#email').val(),
birthday: $('#birthday').val(),
gender: $("input[name='gender']:checked").val(),
photo: photo,
},
success: function(data) {
console.log('pass');
console.log(data);
},
error: function(data) {
console.log('not pass');
console.log(data);
},
cache: false,
contentType: false,
processData: false, <------ i think my error is here
});
如果我离开processData: false,
,帖子到空,通过回显我的数组,所有数据都是空的,在另一种情况下,如果我发表评论,控制台会抛出Uncaught TypeError: Illegal invocation
我需要做的是发送用户输入的值,例如&#34;电子邮件&#34;,&#34;性别&#34;,...和个人资料图片,以更新数据库和将图像保存到文件夹
答案 0 :(得分:0)
这对我来说非常合适
// formData will wrap all files and content of form
var formData=new FormData($('#formId')[0]);
// you can add more data ot formData after this to
$.ajax({
url : 'upload.php',
type : 'post',
data : formData,
processData:false,
contentType:false,
success : function(e)
{
alert('uploaded successfully');
},
error : function()
{
alert('hello from here');
}
});