Ajax返回400错误请求(xhr),admin-ajax.php返回“ 0”?

时间:2019-01-20 21:15:42

标签: php ajax wordpress

所以我在Ajax方面的经验有限,我不确定如何调试/解决问题。有问题的错误是;

  

admin-ajax.php -400错误请求(xhr)。

我已经检查了加载的资源,并且得到的响应为“ 0”。查看它,我可以看到“ 0”响应意味着未设置动作(在ajax数据中)或找不到该动作的回调函数。

看到我已经设置了一个动作,我只能假定这是因为找不到回调函数。但是检查代码在回调中没有错别字吗?

我能得到的任何帮助将不胜感激。

functions.php

add_action('wp_head', function(){   
    require_once( get_stylesheet_directory() .'/inc/better-comments.php' );
    require( get_stylesheet_directory() .'/inc/ajax.php' );
});

ajax.php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

add_action('wp_ajax_nopriv_front_load_more', 'front_load_more');
add_action('wp_ajax_front_load_more', 'front_load_more');

function front_load_more() {

    global $post;
    $post = get_post( $_POST['post_id'] );
    setup_postdata( $post );

    wp_list_comments( array( 'callback' => 'better_comments' ) );

    die();

};

theme.js

jQuery(document).ready(function($){
    $(document).on('click', '.front-load-more', function(){

        var button = $(this);

        // decrease the current comment page value
        cpage--;

        $.ajax({
            url : ajaxurl, // AJAX handler, declared before
            data : {
                'action' : 'front_load_more',
                'post_id': parent_post_id, // the current post
                'cpage' : cpage, // current comment page
            },
            type : 'POST',
            beforeSend : function ( xhr ) {
                button.text('Loading...'); // preloader here
            },
            success : function( data ){
                if( data ) {
                    $('ol.comment-list').append( data );
                    button.text('More comments'); 
                     // if the last page, remove the button
                    if ( cpage == 1 )
                        button.remove();
                } else {
                    button.remove();
                }
            }
        });
        return false;

    });
});

comments.php(触发器)

<?php   
  $cpage = get_query_var('cpage') ? get_query_var('cpage') : 1;

  if( $cpage > 1 ) {
    echo '<a class="btn btn-block btn-soft-primary transition-3d-hover front-load-more">Load More</a>
       <script>
       var ajaxurl = \'' . admin_url('admin-ajax.php') . '\',
            parent_post_id = ' . get_the_ID() . ',
            cpage = ' . $cpage . '
       </script>';
   }
?>
  

已修订-add_action('wp_ajax_nopriv_front_load_more','front_load_more');

     

已修订-add_action('wp_ajax_front_load_more','front_load_more');

     

编辑:21 Jan 15:23   正如@cabrerahector所说,我的ajax.php文件中包含需要移至我的functions.php文件中的代码。这解决了错误的请求问题。

1 个答案:

答案 0 :(得分:1)

问题在于,您在import pandas as pd data = { 'Question': [[['Hi Hello']], [['How are you']], [['Whats up']], [['What is your name']], [['May I know your name?']]], 'Answer':['there', 'there', 'there', 'My name is Thomas', 'My name is Thomas'] } df = pd.DataFrame(data) print(df) new_df = df.groupby('Answer').sum().reset_index() print(new_df) train_file = open('train.csv', 'a') test_file = open('test.csv', 'a') for _, instance in new_df.iterrows(): n_questions = len(instance.Question) splits = int(2 * n_questions / 3) # Assuming you want a train/test split of 3:1 train = instance.Question[:splits] for train_example in train: train_file.write(train_example[0] + ',' + instance.Answer + '\n') test = instance.Question[splits:] for test_example in test: test_file.write(test_example[0] + ',' + instance.Answer + '\n') 中拥有的代码应该在ajax.php中:WordPress未正确注册您的functions.php操作挂钩,因此{{1} }您看到的回复。