我有一个前端搜索框,我为它实现了自动完成,当我登录时,它对我来说非常有用。
但当我没有登录时,由于ajax调用,我得到302,响应头中有位置,所以它试图重定向,我不知道为什么。我注意到很多关于这个问题的帖子,但没有一个帖子/问题对我没有帮助。
我的代码,在用户登录时有效,但在用户未登录时无效,请帮助。
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();
}
}