WordPress.org

Ready to get started?Download WordPress

Forums

WordPress when sending a WP_Ajax_Response the response is not received. (1 post)

  1. MarcSamui
    Member
    Posted 1 year ago #

    Hi All,

    I'm searching for days now for a solution to my problem.
    When I send a WP_Ajax_Response the clientside is not receiving it.
    I checked all possible tips on internet but still I can't get it working.
    Can somebody help me?

    I registered the javascript file:

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

    This is the PHP code:

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

    and this is the javascript file:

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

    While debugging the res content shows responses=[0]. In the jQuery.each function no responses are handled!

    Thanks a lot for any tips that might help me out here!

    Marc

Topic Closed

This topic has been closed to new replies.

About this Topic