Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1323707
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 questionTrying 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 questionTrying 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 |1 Answer
Reset to default 1In 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
版权声明:本文标题:ajax - Unable to parse JSON response from wp_send_json_success 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130340a2422128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
alert("Hello")
statement? Alsoresponse.data.messages
should be used to get the desired data. – Vasanth Gopal Commented Sep 9, 2020 at 4:26