AJAX在未登录时停止工作?

时间:2012-01-12 作者:v3nt

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

已经拥有;add\\u action(\'wp\\u ajax\\u filter\\u schools\',\'filter\\u schools\');add\\u action(\'wp\\u ajax\\u nopriv\\u filter\\u schools\',\'filter\\u schools\');

在函数中。php,任何地方都没有错误。

未登录时得到的响应为
来自safari*请求URL:http://www.payingforit.org.uk/wp-admin/admin-ajax.php?term=holywe&;操作=筛选学校(&U);postType=schoolRequest方法:GETStatus代码:302已找到*

欢迎任何帮助!Dc。

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

});
函数中的函数。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
}

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

Edit: 我在下面保留了我的原始答案,但是,我不确定我在想什么。。。您应该永远不需要触发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

SO网友:v3nt

filter\\u 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;

结束

相关推荐

WordPress AJAX-如何在回调函数中返回True或False

因此,我已经使用Wordpress正确地设置了AJAX,并遵循了一些很好的教程,它工作得很好。有一件事我不太清楚,那就是如何在回调函数中正确地返回TRUE或FALSE。我的php函数如下:function my_callback() { if(something()) { echo \'false\'; } else { echo \'true\'; } exit; } 在JS