简体   繁体   中英

What's the problem with my Wordpress ajax request

I want to add have a form in my Wordpress dashboard that work with ajax but my request response is 400 Bad request

add_action('admin_footer', 'vitrin_admin_ajax');
        function vitrin_admin_ajax() {
        ?>
            <script>
                (function($) {
                    $('.vitrin-private-message-form').submit(function(event) {
                        event.preventDefault();
                        jQuery.ajax({
                            url: '<?php echo admin_url('admin-ajax.php'); ?>',
                            type: 'post',
                            data: {
                                action: 'submit_vitrin_pm',
                                ppmcontent: jQuery('#pmcontent').val(),
                                ppmusers: jQuery('#pmusers').val(),
                            },
                            success: function(data) {
                                // jQuery('#datafetch').html(data);
                                alert(data)
                            }
                        });
                    })
                })(jQuery);
            </script>
        <?php
        }
        ?>

I also add:

add_action('wp_ajax_submit_vitrin_pm', 'submit_vitrin_pm');
add_action('wp_ajax_nopriv_submit_vitrin_pm', 'submit_vitrin_pm');

and

function submit_vitrin_pm() {
    echo 'ssss';
    die();
}

I checked, your ajax response is working, you didn't share your html form, i added a form for check.

add_action('admin_footer', 'vitrin_admin_ajax');
function vitrin_admin_ajax() {
?>

      <form action="#" method="POST" class="vitrin-private-message-form">
        <div id="name-group" class="form-group">
          <label for="name">Name</label>
          <input
            type="text"
            class="form-control"
            id="pmcontent"
            name="name"
            placeholder="Full Name"
          />
        </div>

        <div id="email-group" class="form-group">
          <label for="email">Email</label>
          <input
            type="text"
            class="form-control"
            id="pmusers"
            name="email"
            placeholder="email@example.com"
          />
        </div>

        <button type="submit" class="btn btn-success">
          Submit
        </button>
      </form>
    <script>
        (function($) {
            $('.vitrin-private-message-form').submit(function(event) {
                event.preventDefault();
                console.log('working')
                jQuery.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: {
                        action: 'submit_vitrin_pm',
                        ppmcontent: jQuery('#pmcontent').val(),
                        ppmusers: jQuery('#pmusers').val(),
                    },
                    success: function(data) {
                        // jQuery('#datafetch').html(data);
                        alert(data)
                    }
                });
            })
        })(jQuery);
    </script>
<?php
}


add_action('wp_ajax_submit_vitrin_pm', 'submit_vitrin_pm');
add_action('wp_ajax_nopriv_submit_vitrin_pm', 'submit_vitrin_pm');

function submit_vitrin_pm() {
    echo 'ssss';
    die();
}

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-2025 STACKOOM.COM