通过AJAX表单添加新帖子时请求错误

时间:2019-02-11 作者:GeorgeP

我试图在wordpress插件中插入一篇带有简单ajax表单的新帖子。访问者将使用此表单从前端创建帖子。

我的表格如下:

<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>
这是php ajax函数

function save_post () {
    ini_set(\'display_errors\', 1); 
    error_reporting(\'E_ALL\');

    $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
我做错了什么?

2 个回复
最合适的回答,由SO网友:GeorgeP 整理而成

终于发现了问题。这个action: save_post ajax调用中的数据中缺少。此代码适用于:

  <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>

SO网友:moped

表单调用动作“创建事件”,ajax函数为“save\\u post”。告诉ajax脚本要使用哪个操作(方法/函数)。我认为它永远不会达到“save\\u post”。