Ajax file upload in Wordpress - can't pass FormData

前端 未结 5 891
失恋的感觉
失恋的感觉 2021-01-02 07:13

I\'ve made a script which uses $.ajax and FormData to pass two form objects to PHP. One form object is a text and the other is a file. It worked well as a stand-alone script

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 07:48

    I added

     dataType: 'json'
    

    and it helps. Full listing of your code of Ajax call:

    jQuery.ajax({
        type: 'POST',
        url: fiuajax.ajaxurl,
        data: {
            action: 'fiu_upload_file',
            security: fiuajax.security,
            data: fd,
            dataType: 'json',
            contentType: false,
            processData: false,
        },
        success: function(response){
            var dataObj = jQuery.parseJSON(response);
            if(dataObj.message == 'error') {
                jQuery('.fiu_validation').html('The following error occured: '+dataObj.desc);
            }
            else if(dataObj.message == 'success') {
                jQuery('.fiu_file').val('');
            }
            console.log(response);
        }
    });
    

提交回复
热议问题