php json_encode响应将html发送回ajax jquery请求,而不是json
对于作为wordpress插件的简单联系人表单,我在同一页面中创建了一个作为弹出窗口的表单:php json_encode响应将html发送回ajax jquery请求,而不是json,php,jquery,json,ajax,wordpress,Php,Jquery,Json,Ajax,Wordpress,对于作为wordpress插件的简单联系人表单,我在同一页面中创建了一个作为弹出窗口的表单: <form method="post" id="w_form" enctype="multipart/form-data"> <label for="first_name" class="text-secondary">First Name</label> <input id="first_name" type="text" name="fir
<form method="post" id="w_form" enctype="multipart/form-data">
<label for="first_name" class="text-secondary">First Name</label>
<input id="first_name" type="text" name="first_name" form-control" value="" required="">
<label for="last_name" class="text-secondary">Last Name</label>
<input id="last_name" type="text" name="last_name" form-control" value="" required="">
<button class="btn btn-danger" id="submit" type="submit" value="Submit" name="submit">
Submit
</button>
</form>
传递到同一页面和表单顶部的php代码:
if ( isset( $_POST['first_name'] ) ) {
$uploaded_status;
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$to = 'abc@example.com';
$subject = 'test';
$body = 'body';
if ( wp_mail( $to, $subject, $body, '') ) {
$uploaded_status = 1;
} else {
$uploaded_status = 0;
}
if ( $uploaded_status == 1 ) {
$response = array();
$response['status'] = 1;
$response['message'] = 'Your form submitted successfully!';
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
}
}
此过程工作正常,通过电子邮件将表单数据发送到电子邮件地址,但响应数据为:success
ajax的一部分,获取html内容而不是json,并返回parserror
作为控制台。调试(错误)代码>
正如我提到的,提交和以电子邮件的形式发送数据,正常工作,但我没有提交按钮后控制UI的响应过程的正确数据如果您在响应发送到同一页面的Ajax请求时接收到无效的JSON数据,则需要在发送响应之前丢弃任何输出缓冲区,然后立即终止处理,以防止进一步的错误追加到响应流的内容
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['first_name'],$_POST['last_name'] ){
ob_clean();# discard any previous buffer data
$uploaded_status;
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$to = 'abc@example.com';
$subject = 'test';
$body = 'body';
if ( wp_mail( $to, $subject, $body, '') ) {
$uploaded_status = 1;
} else {
$uploaded_status = 0;
}
if ( $uploaded_status == 1 ) {
$response = array();
$response['status'] = 1;
$response['message'] = 'Your form submitted successfully!';
header("Content-Type: application/json; charset=utf-8", true);
exit( json_encode( $response ) );#terminate
}
}
澄清一下:您将ajax请求发送到同一页面,并作为响应的一部分接收HTML输出-从而使json响应数据无效?@RamRaider是的,我将表单数据作为ajax发送到后端,它使用该数据发送电子邮件,到目前为止工作正常,但在成功通过ajax之后,它无法作为json获得响应,因此没有正确的数据当您收到ajax响应时,在控制台中可以观察到什么?我使用了``error:function(xhr,error){console.debug(xhr);console.debug(error);},“``要检查当我删除jquery部分中的数据类型:“json”时触发的parserror
错误,
不会有错误尽管我们将json_encode($response)设置为php输出,ajax响应是一个没有$response的html我想主要的一点是将数据从php传递给jquey。php正确地完成了主要任务(发送电子邮件),但要传递$respone我们有问题,所以在这一步中没有任何输出,因为php的第一个输出是以html的形式发送电子邮件,尽管我传递了json_encode,但ajax会以响应的形式获取html
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['first_name'],$_POST['last_name'] ){
ob_clean();# discard any previous buffer data
$uploaded_status;
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$to = 'abc@example.com';
$subject = 'test';
$body = 'body';
if ( wp_mail( $to, $subject, $body, '') ) {
$uploaded_status = 1;
} else {
$uploaded_status = 0;
}
if ( $uploaded_status == 1 ) {
$response = array();
$response['status'] = 1;
$response['message'] = 'Your form submitted successfully!';
header("Content-Type: application/json; charset=utf-8", true);
exit( json_encode( $response ) );#terminate
}
}