weixin_33713503 2016-06-16 03:09 采纳率: 0%
浏览 17

用ajax发布文件和数据

I have read in other post on SO, similar to what is happening to me, but I still can not get the solution

$('#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
    });

if i leave processData: false, , the post arrives empty, by making echo of my array, all data is empty, in the other case, if I comment, the console throws Uncaught TypeError: Illegal invocation

I need to do is send the values entered by the user such as "email", "gender", ... and profile picture, to make an update to the database and to save the image to a folder

  • 写回答

1条回答 默认 最新

  • weixin_33726318 2016-06-16 04:34
    关注

    this is what working perfectly for me

    // 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');   
                        }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题