Ajax在未登录时停止工作?


9

有一个自动完成字段已经工作了几个月,但是在未登录时已经停止工作了吗?不确定何时,但在最近几天或一周内(最近未更新wordpress)。

已经有; add_action('wp_ajax_filter_schools','filter_schools'); add_action('wp_ajax_nopriv_filter_schools','filter_schools');

在functions.php中,任何地方都没有错误。

未登录时得到的响应是
从safari中获取... *请求网址:http://www.payingforit.org.uk/wp-admin/admin-ajax.php?term = holywe&action = filter_schools&postType = school请求方法:GET状态码:302找到*

任何帮助欢迎!直流电

jQuery代码

 $( "#userSelectedSchool" ).bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        }).autocomplete({
            source: function( request, response ) {

                $.getJSON( "/wp-admin/admin-ajax.php", {


            term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
            }, response );

            dataToBeSent = {
                term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
            }

            console.log(request.term);

        }, select: function( event, ui ) {

            var terms = split( this.value );
            // remove the current input
            terms.pop();
            // add the selected item
            terms.push( ui.item.id );
            // add placeholder to get the comma-and-space at the end // ui.item.label
            terms.push( "" );
            this.value = ui.item.label;

            $('input[name=userSchool]').val(ui.item.urn)

            return false;

        }, open: function() { $('.ui-menu').width(300) }

});

在functions.php中的功能

add_action('wp_ajax_filter_schools', 'filter_schools');
add_action('wp_ajax_nopriv_filter_schools', 'filter_schools');

function filter_schools(){
    global $wpdb; // this is how you get access to the database

    $str = $_GET['term'];
    $action = $_POST['action'];
    $postType = $_POST['postType'];

    $finalArgs =  array (
        'posts_per_page'=>5,
        'order' => 'ASC',
        'post_type' => 'school'
    );

    $searchSchools = new WP_Query( $finalArgs );
    $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");

    $args = array(
        'post__in'=> $mypostids,
        'post_type'=>'school',
        'orderby'=>'title',
        'order'=>'asc'
    );

    $res = new WP_Query($args);
    while( $res->have_posts() ) : $res->the_post();

        global $post;

        $EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);
        $URN = get_post_meta($post->ID,'URN', true);
        $add = get_post_meta($post->ID,'address', true);

        $schl = array('post_id'=>$post->ID,'id'=>$EstablishmentNumber, 'label'=>$post->post_title.', '.$add['town'].' '.$add['postcode'] , 'value'=>$EstablishmentNumber, 'urn'=>$URN );
        $matchedSchools[] = $schl;

    endwhile;

    echo json_encode($matchedSchools);
    wp_reset_postdata();
    die(); // this is required to return a proper result
}

Answers:


6

编辑:我将原始答案保留在下面,但是,我不确定我在想什么...您永远不需要触发do_action( 'wp_ajax...' )

尽管我不能确定问题是什么,但问题中的代码是否还可以(我认为$_POST应该$_GET与一起使用.getJSON)。


尝试将其放在顶部...

if(isset($_REQUEST['action']) && $_REQUEST['action']=='filter_schools'):
        do_action( 'wp_ajax_' . $_REQUEST['action'] );
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
endif;

我认为WordPress不会为未登录的用户自动执行ajax操作。潜在的非用户可能会做他们本来无法做的事情。

我可能也会将这些$_GETs和%_POSTs 更改为$_REQUEST


感谢斯蒂芬-由那些更新,但它仍然没有工作未登录时。
v3nt

经过一番混乱后,看来do_action必须在add_filter之后。现在工作,所以谢谢斯蒂芬...
v3nt 2012年

非管理员用户也遇到同样的问题。这个解决方案成功了,顺序对我来说也很重要。
brasofilo

2

filter_schools()函数之前的最终工作代码。

if(isset($_REQUEST['action']) && $_REQUEST['action']=='filter_teachers'):
    add_action('wp_ajax_filter_teachers', 'filter_teachers');
    add_action('wp_ajax_nopriv_filter_teachers', 'filter_teachers');
endif;

if(isset($_REQUEST['action'])):
        do_action( 'wp_ajax_' . $_REQUEST['action'] );
        do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
endif;

Daniel,顺序无关紧要:我有一个插件,其中do_actions在顶部,而add_actions在该函数被调用之前。同样,add_actions也不必在'if'语句中。但是,如果上述方法可行,那么它可行!
史蒂芬·哈里斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.