【问题标题】:php json_encode response sends back html to ajax jquery request instead jsonphp json_encode 响应将 html 发送回 ajax jquery 请求而不是 json
【发布时间】:2020-03-30 12:42:30
【问题描述】:

对于作为 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="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>

其数据将通过ajax发送到后端:

$("#w_form").submit( function(event) {

    event.preventDefault();

    $.ajax({

        type: "post",
        data: new FormData(this),
        dataType: 'json',
        contentType: false,
        cache: false,
        processData: false,
        beforeSend: function() {

            $('#submit').attr("disabled", "disabled");

        },
        success: function( response ) {

           alert( response );

           // if ( response.status == 1 ) {

           //    $('#w_form')[0].reset();

           // }


        },
        error: function (xhr, error) {

            console.debug(xhr);

            console.debug(error);

        },

    });

});

在同一页面和表单顶部传递给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);

    }

}

此过程正常工作,并通过电子邮件将表单数据发送到电子邮件地址,但响应数据为 ajax 的 :success 部分,获取 html 内容而不是 json 并返回 parsererrorconsole.debug(error);

正如我所提到的,以电子邮件形式提交和发送数据可以正常工作,但我没有正确的数据用于响应过程在提交按钮后控制 UI

【问题讨论】:

  • 澄清:您将 ajax 请求发送到同一页面并接收 HTML 输出作为响应的一部分 - 从而使 json 响应数据无效?
  • @RamRaider 是的,我将表单数据作为 ajax 发送到后端,它使用该数据发送电子邮件,到目前为止它工作正常,但是在成功通过 ajax 后,它无法以 json 形式获得响应,所以不正确数据

标签: php jquery json ajax wordpress


【解决方案1】:

如果您收到无效的 JSON 数据以响应发送到同一页面的 Ajax 请求,您需要在发送响应之前丢弃任何输出缓冲区,然后立即终止处理以防止将任何进一步的内容附加到响应流.

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 响应时,您可以在控制台中观察到什么?
  • 我使用了```error: function (xhr, error) { console.debug(xhr);控制台.调试(错误); }, ``` 检查触发parsererror的错误
  • 当我在 jquery 部分删除 dataType: 'json', 时,虽然我们将 json_encode( $response ) 设置为 php 输出,但不会有错误,ajax 响应是一个没有 $response 的 html
  • 我认为重点是将数据从 php 传递到 jquey。 php 正确执行主要任务(发送电子邮件),但是要传递 $respone 我们有问题,所以在这一步中没有任何输出
  • 我猜因为 php 的第一个输出是以 html 格式发送电子邮件,虽然我之后传递了 json_encode,但 ajax 将 html 作为响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2014-10-24
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多