admin管理员组

文章数量:1122832

I've defined a PHP callback function to handle the communication between server-side and client-side:

function json_render_modal_body() {

    check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' );

    $response = array( 'text' => 'no' );
    $modal_id = null;

    if ( isset( $_GET['modal_id'] ) && ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) {
        wp_send_json_error( $response );
    }

    if ( ! $form = render_form( $modal_id ) ) {
        wp_send_json_error( $response );
    }

    $response['text'] = 'yes';

    wp_send_json_success( $response );

}

I've told WordPress about this function, to handle the communication process:

add_action( 'wp_ajax_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );
add_action( 'wp_ajax_nopriv_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );

I've registered/enqueued/localized my JS script to handle the AJAX request.

This is what I've localized:

wp_localize_script(
    THEME_PREFIX . '-modals',
    'mbe_theme_modal',
    array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'nonce' => wp_create_nonce( THEME_PREFIX . '-modals' )
    )
);

This is what my JS script looks like:

var modal_id = jQuery( e.relatedTarget ).attr( "data-target" ).replace( "#", "" );

jQuery.get(
    mbe_theme_modal.ajax_url,
    {
        action: "json_render_modal_body",
        modal_id: modal_id,
        nonce: mbe_theme_modal.nonce
    },
    function ( data ) {

        console.log( data );

    },
    'json'
);

Any idea why I keep getting a 0 in my AJAX response?

I've also tried removing the nonce stuff from my code, and accessing the direct URL (domain/wp-admin/admin-ajax.php?action=json_render_modal_body&modal_id=some-modal-id, however, I still continue to get a 0.

I've even tried keeping the PHP function super simple with nothing but a simple text response, and still continue to get a 0.

I've defined a PHP callback function to handle the communication between server-side and client-side:

function json_render_modal_body() {

    check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' );

    $response = array( 'text' => 'no' );
    $modal_id = null;

    if ( isset( $_GET['modal_id'] ) && ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) {
        wp_send_json_error( $response );
    }

    if ( ! $form = render_form( $modal_id ) ) {
        wp_send_json_error( $response );
    }

    $response['text'] = 'yes';

    wp_send_json_success( $response );

}

I've told WordPress about this function, to handle the communication process:

add_action( 'wp_ajax_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );
add_action( 'wp_ajax_nopriv_json_render_modal_body', __NAMESPACE__ . '\json_render_modal_body' );

I've registered/enqueued/localized my JS script to handle the AJAX request.

This is what I've localized:

wp_localize_script(
    THEME_PREFIX . '-modals',
    'mbe_theme_modal',
    array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'nonce' => wp_create_nonce( THEME_PREFIX . '-modals' )
    )
);

This is what my JS script looks like:

var modal_id = jQuery( e.relatedTarget ).attr( "data-target" ).replace( "#", "" );

jQuery.get(
    mbe_theme_modal.ajax_url,
    {
        action: "json_render_modal_body",
        modal_id: modal_id,
        nonce: mbe_theme_modal.nonce
    },
    function ( data ) {

        console.log( data );

    },
    'json'
);

Any idea why I keep getting a 0 in my AJAX response?

I've also tried removing the nonce stuff from my code, and accessing the direct URL (domain.com/wp-admin/admin-ajax.php?action=json_render_modal_body&modal_id=some-modal-id, however, I still continue to get a 0.

I've even tried keeping the PHP function super simple with nothing but a simple text response, and still continue to get a 0.

Share Improve this question asked Jun 12, 2017 at 21:16 Michael EcklundMichael Ecklund 6,81213 gold badges68 silver badges110 bronze badges 4
  • 4 Have you considered using the REST API instead of WP AJAX? – Tom J Nowell Commented Jun 12, 2017 at 21:26
  • 1 Seeing just 0 typically means there is no function currently hooked to that action. Make sure your add_action calls are getting executed on ajax requests. – Milo Commented Jun 12, 2017 at 23:15
  • 1 a guess, the namspace is not right – Mark Kaplun Commented Jun 13, 2017 at 3:43
  • @TomJNowell I'd like to use the REST API if it'll get the job done. Could you show me a RESTful example as an answer to this question, or should I ask a new question? – Michael Ecklund Commented Jun 14, 2017 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 0

Usually, you can use the global variable ajaxurl instead of any path using admin-ajax.php.

More importantly, the PHP function should echo the response before calling wp_die(). Because you haven't called wp_die(), AJAX is probably waiting for more from PHP.

Hope this helps.

P.S. What is that 'json' string doing where the AJAX fail() function should be?

本文标签: jqueryHow to Use JSON With AJAX