簡體   English   中英

使用ajax發布文件和數據

[英]Post File and data with ajax

我在其他帖子中讀過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

我需要做的是發送用戶輸入的值,如“email”,“gender”,...和個人資料圖片,以更新數據庫並將圖像保存到文件夾

這對我來說非常合適

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM