简体   繁体   English

在没有页面重新加载的情况下在 wordpress 中提交 Ajax 表单总是给我以下错误:400 bad request

[英]Ajax form submittion in wordpress without page reloading always give me the following error: 400 bad request

I am building a custom template in WordPress and I want to handle form submission using Ajax without page reloading and I always have the following error on my console 400 bad request.我正在 WordPress 中构建一个自定义模板,我想在不重新加载页面的情况下使用 Ajax 处理表单提交,并且我的控制台 400 错误请求总是出现以下错误。 The form is not submitting to the server in other to handle the request.表单没有提交到其他服务器来处理请求。 I have tried many possibilities but I didn't succeed.我尝试了很多可能性,但没有成功。

Bellow is the form,is an image upload form波纹管是表格,是图片上传表格

<form enctype="multipart/form-data" method="post" action="">
   <div class="Success-div"></div>
      <p> Vous devez nous fournir un certificat médical valide attestant vos aptitudes à 
       faire partir du club et faire part aux activités du club:</p>
      <p class="statusMsg"></p>
 <div class="form-group">
 <label for="tel">Certificat médical</label><a style= "color:#DAA520;" href="#" 
 id="effectuerTest"> (je ne possède pas un certificat médical)</a>
    <input type="file" name="certificatMedicalMajeur" class="form-control" 
 id="CertificatMedicalfichier" accept="application/pdf" required/>
    </div>
    <input type="submit" name="certificat-majeur-submit" id="submitbtn" class="certificat- 
   majeur-submit btn btn-primary pull-right" value="Enregistrer" />
</form>

Ajax code TO send the submitted data to the server Ajax 代码 TO 将提交的数据发送到服务器

jQuery(document).on('click', '.certificat-majeur-submit', function (e) {
e.preventDefault();
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var file_data =  $('#CertificatMedicalfichier').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
var $this = $(this);
  jQuery.ajax({
    type: 'POST',
    data: {
      data:form_data,
      action: 'post_md_support_save'
    },
    url: ajaxurl,
    processData : false,
    success: function (response) {
        jQuery('.Success-div').html(data.message);
    },  
    error: function (response) {
      console.log("error form");
    }
    });
});

And finally this is the code to handle the request file in function.php最后这是在function.php中处理请求文件的代码

add_action( 'wp_ajax_post_md_support_save', 'md_abonnements_save' );

add_action( 'wp_ajax_post_md_support_save', 'md_abonnements_save' );

function md_abonnements_save(){

    echo "ajax responding";
    die();
}

如果您在网站的前端使用此代码,您还应该添加 nopriv 操作挂钩: add_action( 'wp_ajax_nopriv_post_md_support_save', 'md_abonnements_save' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM