简体   繁体   中英

POST http://localhost/website/wp-admin/admin-ajax.php 400 (Bad Request)

I need to take image input from a user and save it into a WordPress website database with AJAX without any plugin. How can I sort out the issue?

//function.php

add_action('wp_head', 'myplugin_ajaxurl');

function myplugin_ajaxurl() {
   echo '<script type="text/javascript">
           var ajaxurl = "' . admin_url('admin-ajax.php') . '";
         </script>';

}
//ajax code

jQuery(document).ready(function(){
    jQuery('#fupForm').on('submit', function(e){
        e.preventDefault();
      //  var file = jQuery("file#file").val();

      var fd = new FormData(this);
       var files = jQuery('#image')[0].files[0];
       fd.append('file',files);
        jQuery.ajax({
              url: ajaxurl,
              type: "POST",
              data : {  
                     files,
                     action:'my_ajax_callback_function',
                     },
                     contentType: false,
                     processData: false,
              beforeSend: function(){
                jQuery('#fupForm').css("opacity",".5");
            },
              success: function(response){
                  alert(response);
                  jQuery('#fupForm').css("opacity","");
              } 
        });
    });
});

There are two posts that have exactly your doubt, see if that solves for you anything just warn that I will try to help you

Wordpress and AJAX - Upload image as featured or https://wordpress.stackexchange.com/questions/198781/wordpress-ajax-file-upload-frontend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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