在 Wordpress 中创建新 post 时出现错误请求

Bad Request when creating new post in Wordpress

我正在尝试在 wordpress 插件中插入一个带有简单 ajax 形式的新 post。访问者将使用此表单从前端创建 posts。

我的表格是这样的:

<script>
jQuery( document ).ready(function() {
    jQuery('input[type=submit]').on('click', function() {
        var ajaxurl = 'https://example.com/wp-admin/admin-ajax.php';
        jQuery.ajax({
            type: "POST",
            data: jQuery('.event-form').serialize(),
            url: ajaxurl,
            success: function(result) {
                console.log('data sent!');
                console.log('sent to: ' + templateDir + loadUrl );
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
        });
    });
});
</script>

<form id="msform" name="review_form" class="form-horizontal event-form" action="/create-event" method="POST" enctype="multipart/form-data">
        <input type="text" name="title" placeholder="title">
        <textarea rows="4" name="desc" placeholder="Description"></textarea>
        <select name="category">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="opel">Opel</option>
          <option value="audi">Audi</option>
        </select>
        <input type="hidden" name="userID" id="userID" value="<?php get_current_user_id(); ?>">
        <input type="submit" name="submit" class="submit action-button" value="Submit"/>
</form>

这是phpajax函数

function save_post () {

    $new_post = array(
                'post_title'    => $_POST['title'],
                'post_author'   => $_POST['userID'],
                'post_content'  => $_POST['desc'],
                'post_category' => array($_POST['category']),
                'post_status'   => 'publish',           
                'post_type' => 'post'  
        );

    $post_id = wp_insert_post($new_post);
}
add_action('wp_ajax_nopriv_save_post','save_post');
add_action('wp_ajax_save_post','save_post');

单击提交按钮时,我在控制台中收到以下错误消息:

[object Object] :: error :: Bad Request

我做错了什么?

试试这个代码。 action: 'save_post' 在 ajax 脚本中缺失

function save_post () {

    $params = array();
    parse_str($_POST['data'], $params);

    $new_post = array(
                'post_title'    => $params['title'],
                'post_author'   => $params['userID'],
                'post_content'  => $params['desc'],
                'post_category' => array($params['category']),
                'post_status'   => 'publish',           
                'post_type' => 'post'  
        );

    $post_id = wp_insert_post($new_post);
    die($post_id);
}
add_action('wp_ajax_nopriv_save_post','save_post');
add_action('wp_ajax_save_post','save_post');


    <script>
    jQuery( document ).ready(function() {
        jQuery('input[type=button]').on('click', function() {
            var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
            jQuery.ajax({
                type: "POST",
                data: {
                    data:jQuery('.event-form').serialize(),
                    action: 'save_post'
                },
                url: ajaxurl,
                success: function(result) {
                    console.log('data sent!');
                    console.log('sent to: ' + templateDir + loadUrl );
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
            });
        });
    });
    </script>

    <form id="msform" name="review_form" class="form-horizontal event-form" method="POST" enctype="multipart/form-data">
            <p><input type="text" name="title" placeholder="title" value="test"></p>
            <textarea rows="4" name="desc" placeholder="Description"></textarea>
            <select name="category">
              <option value="volvo">Volvo</option>
              <option value="saab">Saab</option>
              <option value="opel">Opel</option>
              <option value="audi">Audi</option>
            </select>
            <input type="hidden" name="userID" id="userID" value="<?php echo get_current_user_id(); ?>">
            <input type="button" name="submit" class="submit action-button" value="Submit"/>
    </form>