如何查看ajax.php中的错误

问题描述 投票:0回答:1

我通过AJAX请求调用ajax.php,并在响应时尝试解析JSON。我在mail()中有一个ajax.php函数,这会导致错误。我需要看看它是什么错误。真是太不可思议了。.我已经为它苦苦挣扎了好几个小时。

[当我通过浏览器访问ajax.php时,mail()有效,并且能够接收电子邮件。但是,当我通过AJAX呼叫并读取响应时,它给出的是parserror,而且还不足以找到原因。这是简化的代码:

ajax.php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
error_log("test");

goto mail;

//skipped code

mail:
try {

    $message = 'Test Mail';
    $from = "[email protected]";
    $to = "[email protected]";
    $subject = "Application";
    $headers = "From:" . $from;
    mail($to, $subject, $message, $headers);
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}

if(count($errors)>0){
    echo json_encode(array('status'=>'error','message'=>implode($errors,'<br>')));
}
die();

global.js

$('#theForm').on('submit',function(e){
    e.preventDefault();

    let formData = new FormData();

    formData.append('fname',$('#theForm input[name=first_name]').val());
    //... appending lots of data + file

    $.ajax({
        contentType: false,
        processData: false,
        cache: false,
        type: 'POST',
        dataType: 'json',
        url: '/lib/ajax.php',
        data: formData,
        success: function (data) {
            //celebrate
        },
        error: function (XMLHttpRequest, textStatus, errorThrown){
            $('.submit-result').html(textStatus);
        },
        timeout: 5000
    });
});

PS。我希望我可以有error_log,以便可以看到PHP错误。它是共享主机上的子域,因此我无法编辑php.ini。但是它确实在主域上创建了error_log。

php jquery json ajax email
1个回答
0
投票

[如果您收到Parse error消息,则意味着脚本输出的不仅仅是JSON数组。您可以使用output buffering解决该问题-并查看内容是什么。将ob_start()添加到脚本的开头,然后在回显数组即[>]时使用ob_start()收集输出。

ob_get_clean()

然后在您的JS中,您可以ob_get_clean() /ob_start(); ini_set('display_errors', 1); error_reporting(E_ALL); ... echo json_encode(array('status'=>'error', 'message'=>implode($errors,'<br>'), 'output' => ob_get_clean())); console.log

© www.soinside.com 2019 - 2024. All rights reserved.