Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery Ajax文件上传表单数据_Jquery_Ajax_Form Data - Fatal编程技术网

jQuery Ajax文件上传表单数据

jQuery Ajax文件上传表单数据,jquery,ajax,form-data,Jquery,Ajax,Form Data,我想知道为什么当我使用serialize时它工作得很好,但是如果我尝试使用formData,那么我的Ajax似乎不是“服务器端”的 我的HTML: <form method="post" class="bg-light p-3 h-100 audio" enctype="multipart/form-data"> <input type="hid

我想知道为什么当我使用serialize时它工作得很好,但是如果我尝试使用formData,那么我的Ajax似乎不是“服务器端”的

我的HTML:

<form method="post" class="bg-light p-3 h-100 audio" enctype="multipart/form-data">
                                <input type="hidden" name="tab" value="tab7">

                                <fieldset>
                                    <legend class="legend clearfix">Médias
                                        <button type="button" id="addAudio" class="btn btn-first rounded-0 d-block float-right" title="Ajouter un autre fichier">
                                            <i class="far fa-plus"></i>
                                        </button>
                                    </legend>

                                    
                                    <label for="video_file" class="col-form-label col-form-label-sm">Echantillon video <small class="text-muted">(5 Mo, mpeg/webm)</small></label>
                                    <input type="file" name="video_file" id="video_file" class="form-control-file form-control-sm mb-1 video" data-video-item="" accept="video/mpeg,video/webm">

梅迪亚斯
Echantillon视频(5个月,mpeg/webm)
以下是我使用的jQuery:

if($(this).data('changed') == 'yes'){
                var form = $(this).closest('form');
                var formdt = form.serialize();
                var id = <?=($id) ?? '';?>;
                var phpform = '<?=$form;?>';
                

                $.post('<?=$this->url('ajax_products_edit')?>', {'form': formdt, 'id': id, 'phpform': phpform}, function(json){
                    console.log(json);
                    if(json['result'] == 'success'){
                        alert(json.msg); // A VIRER DES QUE LE BUG DE LA SWAL QUI SE ZAP TOUTE SEUL SERA FIX
                        swal("Modifié", json.msg, "success");                       
                    }
                    else if(json.result == 'error') {
                        alert(json.msg); // A VIRER DES QUE LE BUG DE LA SWAL QUI SE ZAP TOUTE SEUL SERA FIX
                        swal( "Oops", json.msg , "error");
                    } 
                    else {
                        alert(json.msg); // A VIRER DES QUE LE BUG DE LA SWAL QUI SE ZAP TOUTE SEUL SERA FIX
                        swal( "Oops", 'Erreur lors de la modification' , "error");
                    }
                })
            }
if($(this).data('changed')=='yes'){
var form=$(this).closest('form');
var formdt=form.serialize();
变量id=;
var phpform='';
$.post(“”,{'form':formdt,'id':id,'phpform':phpform},函数(json){
log(json);
如果(json['result']=='success'){
alert(json.msg);//一个绿色的小虫子在游泳的时候会发出刺耳的声音
swal(“Modifié”,json.msg,“success”);
}
else if(json.result==“error”){
alert(json.msg);//一个绿色的小虫子在游泳的时候会发出刺耳的声音
swal(“Oops”,json.msg,“error”);
} 
否则{
alert(json.msg);//一个绿色的小虫子在游泳的时候会发出刺耳的声音
swal(“Oops”,“Erreur lors de la modification”,“error”);
}
})
}
正如我所说,如果我替换
var formdt=form.serialize()
by
var formdt=new formData(form)
什么都没有发生,我以前从未使用过formData,可能我错过了一个设置或什么

有人能告诉我这件事吗

谢谢你的时间

…如果我替换
var formdt=form.serialize()
var formdt=new formData(form)

是的,有些事情发生了您没有注意到,因为您没有检查

TypeError:构造“FormData”失败:参数1不是“HTMLFormElement”类型

那就是。。。因为您传递了一个jQuery对象。
还记得那句台词吗
var form=$(this).closest('form')

现在,这将很好地工作:

var formdt = new formData(form[0])

)()

这回答了你的问题吗。更具体的回答。如果我正确理解了他的回答(Moh Arjmandi的),我的问题来自于使用.post methode而不是具有更具体设置的.ajax?因为当我阅读其他评论时,它们似乎有相反的问题,formData对它们有效,而serialize则不行。