我正在寻找天,以解决我的问题。 当我发送WP_Ajax_Response时,客户端没有收到它。 我检查了互联网上所有可能的提示,但仍然无法正常工作。 有人能帮助我吗?Wordpress发送WP_Ajax_Response时未收到响应
我注册的javascript文件:
wp_register_script('test-ajax-js',get_template_directory_uri() . '/library/script/ajax.test.js', array('jquery', 'wp-ajax-response'), '1.0');
wp_enqueue_script('test-ajax-js');
wp_localize_script('test-ajax-js', 'ajaxtest',
array(
'Ajax_Url' => admin_url('admin-ajax.php'),
'deshopNonce' => wp_create_nonce('deshop-ajax-test-nonce')
)
);
这是PHP代码:
add_action('wp_ajax_shop_ajax_test', 'test_function');
add_action('wp_ajax_nopriv_shop_ajax_test', 'test_function');
function test_function() {
check_ajax_referer('deshop-ajax-test-nonce');
$response = array('what'=>'validate_user_login',
'action'=>'validate_user_login_results',
'id'=>'1',
'data'=>'OK'
);
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();
exit();
}
,这是JavaScript文件:
jQuery(document).ready(function(event) {
var post = {
action: 'shop_ajax_test',
_ajax_nonce: ajaxtest.deshopNonce
};
jQuery.post(ajaxtest.Ajax_Url, post, function(response){
var res = wpAjax.parseAjaxResponse(response, 'ajax-response');
jQuery.each(res.responses, function() {
var a = 0;
});
});
});
在调试资源内容显示响应= [0]。在jQuery.each函数中没有处理任何响应!
非常感谢任何提示,可能会帮助我在这里!
马克