小编Ste*_*fan的帖子

在Wordpress中上传Ajax文件 - 无法传递FormData

我创建了一个脚本,它使用$ .ajax和FormData将两个表单对象传递给PHP.一个表单对象是文本,另一个是文件.它作为一个独立的脚本运行良好.但是,在我将它添加到Wordpress后,作为一个插件,它一直在给我"Uncaught TypeError: Illegal invocation".

我不能序列化formdata,因为那时我将无法将文件传递给PHP中的回调函数.

JS在ajax调用之前涉及FormData:

var fd = new FormData();
var file = jQuery(this).find('input[type="file"]');
var caption = jQuery(this).find('input[name=img_caption]');
var individual_file = file[0].files[0];
fd.append("file", individual_file);
var individual_capt = caption.val();
fd.append("caption", individual_capt);
Run Code Online (Sandbox Code Playgroud)

以上部分是100%正确的.

Ajax调用:

jQuery.ajax({
    type: 'POST',
    url: fiuajax.ajaxurl,
    data: {
        action: 'fiu_upload_file',
        security: fiuajax.security,
        data: fd,
        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);
    }
}); …
Run Code Online (Sandbox Code Playgroud)

php ajax wordpress jquery file-upload

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

ajax ×1

file-upload ×1

jquery ×1

php ×1

wordpress ×1