0

我正在寻找几天来解决我的问题。当我发送 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;
                });
            });

});

调试 res 内容时显示响应 = [0]。在 jQuery.each 函数中没有响应被处理!

非常感谢您提供的任何可能对我有所帮助的提示!

马克

4

2 回答 2

0

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

此外,您是否使用过 Firefox 的 firebug 或 Chrome 的检查器,显然它们会向您显示请求和响应(或者为什么它没有到达那里)?

于 2013-08-11T09:32:31.147 回答
0

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

于 2013-04-09T18:35:12.830 回答