admin管理员组

文章数量:1323530

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Trying to do this the WordPress way: wp_send_json_success

PHP processing save_form return:

        $response = array( 'messages' => $message );
        wp_send_json_success( $response );

jQuery:

        $j.ajax({
            type: 'POST',
            url: ajax_url,
            data: {
                    action:         'save_form',
                    form_id:        $j('#form_id').val(),
                    full_name:      $j('#full_name').val(),
                    email:          $j('#email').val(),
                    signup_nonce:   $j('#signup_nonce').val()
                },
            success: function (response) {
                    console.log(response.messages);
                },
            error: function (response) { }
        });
        return false;

Response in the Inspector:

JSON:
success: true
data: Object { messages: "Enter a valid email address" }
    messages: "Enter a valid email address" 

but...

console.log(response.messages);

returns nothing.

What am I missing?

Thanks, Brad

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

Trying to do this the WordPress way: wp_send_json_success

PHP processing save_form return:

        $response = array( 'messages' => $message );
        wp_send_json_success( $response );

jQuery:

        $j.ajax({
            type: 'POST',
            url: ajax_url,
            data: {
                    action:         'save_form',
                    form_id:        $j('#form_id').val(),
                    full_name:      $j('#full_name').val(),
                    email:          $j('#email').val(),
                    signup_nonce:   $j('#signup_nonce').val()
                },
            success: function (response) {
                    console.log(response.messages);
                },
            error: function (response) { }
        });
        return false;

Response in the Inspector:

JSON:
success: true
data: Object { messages: "Enter a valid email address" }
    messages: "Enter a valid email address" 

but...

console.log(response.messages);

returns nothing.

What am I missing?

Thanks, Brad

Share Improve this question asked Sep 9, 2020 at 0:48 breadwildbreadwild 3915 silver badges22 bronze badges 1
  • As asked by Sally, can you help edit the code to include the alert("Hello") statement? Also response.data.messages should be used to get the desired data. – Vasanth Gopal Commented Sep 9, 2020 at 4:26
Add a comment  | 

1 Answer 1

Reset to default 1

In short, you should use response.data.messages.

And that's because wp_send_json_success() will send a JSON response (an object) with the property data set to whatever that you passed to the function.

wp_send_json_success( 123 );
// In JS, response.data would be an integer. I.e. response.data = 123

wp_send_json_success( array( 'foo' => 'bar' ) );
// In JS, response.data would be an object. So response.data.foo = 'bar'

本文标签: ajaxUnable to parse JSON response from wpsendjsonsuccess