Wordpress发送WP_Ajax_响应时,未收到响应

Wordpress发送WP_Ajax_响应时,未收到响应,ajax,wordpress,response,Ajax,Wordpress,Response,我已经找了好几天的时间来解决我的问题。 当我发送WP_Ajax_响应时,客户端没有收到它。 我在网上查了所有可能的提示,但还是没法让它工作。 有人能帮我吗 我注册了javascript文件: wp_register_script('test-ajax-js',get_template_directory_uri() . '/library/script/ajax.test.js', array( 'jquery', 'wp-ajax-response' ), '1.0' ); wp_enqueu

我已经找了好几天的时间来解决我的问题。 当我发送WP_Ajax_响应时,客户端没有收到它。 我在网上查了所有可能的提示,但还是没法让它工作。 有人能帮我吗

我注册了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' )
                    ) 
            );
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;
                });
            });

});
这是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文件:

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

});
调试res时,内容显示响应=[0]。在jQuery.each函数中,不处理任何响应

非常感谢你给我的建议,这些建议可能会对我有所帮助


Marc

我遇到了同样的问题,但我通过使用jQuery.ajax()而不是post()解决了它
这里有一个链接可能

域、子域或协议是否可能不同?这将终止Ajax响应,因为浏览器不会发送它

另外,您是否使用过Firefox的firebug或Chrome的inspector,因为很明显他们会向您显示请求和响应(或者为什么它没有到达那里)