尝试通过WordPress的方式执行此操作:wp_send_json_success
PHP处理save_form
退货:
$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;
检验员回复:
JSON:
success: true
data: Object { messages: "Enter a valid email address" }
messages: "Enter a valid email address"
但是。。。
console.log(response.messages);
不返回任何内容。
我错过了什么?
谢谢Brad
最合适的回答,由SO网友:Sally CJ 整理而成
简而言之,您应该使用response.data.messages
.
那是因为wp_send_json_success()
将发送带有属性的JSON响应(对象)data
设置为传递给函数的任何内容。
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\'