最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - WordPress ajax call for not logged in users, doesn't work

programmeradmin1浏览0评论

I have on frontend search box, I have implement autocomplete for it, and when I'm logged in, it works great for me.

But when I'm not logged in, I get 302 as result of ajax call, response headers have location in it, so it is trying to redirect, I do not know why. I notice a lot of posts around this issue, but none of the posts/questions didn't help me.

My Code, that works when users are logged in, but doesn't work when users are not logged in, please help.

jQuery

$.ajax({
    type: 'POST',
    url: MyAjax.ajaxurl,
    data: {
        action : 'myajax-submit',
        term : request.term,
        _ajax_nonce : MyAjax.ajax_nonce
    },
    dataType: "json",
    beforeSend: function(jqXHR, settings){
    },
    success: function(data, textStatus, jqXHR){
        response(data);
    },
    error: function(jqXHR, textStatus, errorThrown){
        response([{
            'value':'Error retriveing data',
            'id':1
        }]);
    },
    complete: function(jqXHR, textStatus){
    },
    statusCode: {                    
    }
});

php

add_action('wp_ajax_myajax-submit', 'myajax_submit');


add_action('wp_ajax_nopriv_myajax-submit', 'myajax_submit');
 
    function myajax_submit() {
        global $wpdb;
    
        //check_ajax_referer('myajax_nonce', '_ajax_nonce');
    
        if (isset($_POST["term"])) {
            $q = strtolower($_POST["term"]);

            .....
    
            $json_response = array();
    
            ...
    
            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: text/x-json");
            
            print json_encode($json_response);
            
            die();
        }
}

I have on frontend search box, I have implement autocomplete for it, and when I'm logged in, it works great for me.

But when I'm not logged in, I get 302 as result of ajax call, response headers have location in it, so it is trying to redirect, I do not know why. I notice a lot of posts around this issue, but none of the posts/questions didn't help me.

My Code, that works when users are logged in, but doesn't work when users are not logged in, please help.

jQuery

$.ajax({
    type: 'POST',
    url: MyAjax.ajaxurl,
    data: {
        action : 'myajax-submit',
        term : request.term,
        _ajax_nonce : MyAjax.ajax_nonce
    },
    dataType: "json",
    beforeSend: function(jqXHR, settings){
    },
    success: function(data, textStatus, jqXHR){
        response(data);
    },
    error: function(jqXHR, textStatus, errorThrown){
        response([{
            'value':'Error retriveing data',
            'id':1
        }]);
    },
    complete: function(jqXHR, textStatus){
    },
    statusCode: {                    
    }
});

php

add_action('wp_ajax_myajax-submit', 'myajax_submit');


add_action('wp_ajax_nopriv_myajax-submit', 'myajax_submit');
 
    function myajax_submit() {
        global $wpdb;
    
        //check_ajax_referer('myajax_nonce', '_ajax_nonce');
    
        if (isset($_POST["term"])) {
            $q = strtolower($_POST["term"]);

            .....
    
            $json_response = array();
    
            ...
    
            header("Cache-Control: no-cache, must-revalidate");
            header("Pragma: no-cache");
            header("Content-type: text/x-json");
            
            print json_encode($json_response);
            
            die();
        }
}
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Mar 2, 2012 at 14:44 user1147user1147 8024 gold badges14 silver badges26 bronze badges 3
  • 1 Do you have the final } on myajax_submit? – mor7ifer Commented Mar 2, 2012 at 15:20
  • @m0r7if3r Hi m0r7if3r I have, it was just copy-paste so I accidentally didn't copy last bracket. Tnx. – user1147 Commented Mar 2, 2012 at 15:36
  • I think you should use "nopriv" : add_action('wp_ajax_nopriv_myhandle', 'myfunction'); – user1588779 Commented Feb 17, 2021 at 17:22
Add a comment  | 

2 Answers 2

Reset to default 4

Sounds like you have an issue with another plugin or function that is trying to prevent non logged in or non admin users access to the wp-admin area so it is redirecting from wp-admin/admin-ajax.php and giving you your 302 response.

You need to find the code that is doing this and add a conditional not to redirect if the DOING_AJAX constant is defined.

A non-admin request... still is an admin request

Inside admin-ajax.php the constant WP_ADMIN is set to TRUE. So if you're including your files wrapped inside for example if ( ! is_admin() ), then your request will abort at the beginning of admin-ajax.php.

发布评论

评论列表(0)

  1. 暂无评论