【问题标题】:Php is adding error/notice log to ajax request responsePhp 正在向 ajax 请求响应添加错误/通知日志
【发布时间】:2016-05-18 16:31:47
【问题描述】:

我有这个 ajax 请求:

$.ajax({
    type: 'POST',
    url: 'contactUsInsertBoat.php',
    data: {
        name: $insertBoatForm.find( 'input[name=name]' ).val(),
        phone: $insertBoatForm.find( 'input[name=phone]' ).val(),
        email: $insertBoatForm.find( 'input[name=email]' ).val(),
    },
    success:function(data){
        // successful request;
        var json_obj=JSON.parse(data);
        var $insertBoatContent = $( '#insert-boat-content' );
        $insertBoatContent.addClass("center");
        if (json_obj["response"] == true) {
            $insertBoatContent.html( "<br/><h4>Richiesta inviata con successo</h4>" );
        } else {
            $insertBoatContent.html( "<br/><h4>Richiesta non inviata</h4>" );
        }
    },
    error:function(){
        // failed request; give feedback to user
        $('#ajax-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
    }
});

contactUsInsertBoat.php 我这样做:

include 'functions.php';
require_once 'BusinessLogic/Manager.php';

use BusinessLogic\Manager;

sec_session_start();

if(isset($_POST['name'])){
    Manager::contactInsertBoats($_POST['name'],$_POST['email'],$_POST['phone']);
    $arrResult = array ('response'=>true);
    echo json_encode($arrResult);
}else{
    $arrResult = array ('response'=>false);
    echo json_encode($arrResult);
}

但在那之后我做了这个萤火虫控制台写道:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data


var json_obj=JSON.parse(data);

JSON 的内容是:

<br />
<b>Strict Standards</b>:  Non-static method BusinessLogic\Manager::sendMail() should not be called statically in <b>/var/www/public/BusinessLogic/Manager.php</b> on line <b>1957</b><br />
{"response":true}

为什么要将严格标准的日志添加到 JSON 对象?

【问题讨论】:

  • 就像错误所说的那样,您将sendMail() 作为静态方法调用,但事实并非如此。问题出在您的 Manager.php 文件中。
  • @TheDrot 也许我不够清楚:我不明白为什么要将此消息放在 JSON obj 的答案中。但这不是错误:它只是一条“严格标准”消息,不会阻止执行
  • 然后在顶部添加ini_set('display_errors', '0'); 以隐藏错误。但我认为你应该解决这些问题。
  • @TheDrot 非常感谢,它解决了这个问题:) 我想知道为什么日志错误会进入 ajax 数据响应中,你知道吗?
  • 因为错误/通知会以 html 的形式回显,它会与您的 json 一起将其传递回 ajax 响应。

标签: php jquery ajax


【解决方案1】:

方法contactInsertBoats 不是类Manager 中的静态方法。因此,您需要创建一个Manager 的实例,然后调用该方法。

请对您的 PHP 代码做一个简单的更改:

include 'functions.php';
require_once 'BusinessLogic/Manager.php';

use BusinessLogic\Manager;

sec_session_start();

if(isset($_POST['name'])){
$manager = new Manager;    
$manager->contactInsertBoats($_POST['name'],$_POST['email'],$_POST['phone']);
    $arrResult = array ('response'=>true);
    echo json_encode($arrResult);
}else{
    $arrResult = array ('response'=>false);
    echo json_encode($arrResult);
}

但是,您可以设置 error_reporting(0);在您的 PHP 文件的开头,以防止显示通知/警告/错误/...

【讨论】:

    【解决方案2】:

    因为您将错误报告设置为 E_ALL 并将 display_errors 设置为“On”或 1,这会将包括 E_STRICT 在内的所有错误显示到输出中

    http://php.net/manual/ro/function.error-reporting.php

    最佳做法是记录所有但禁用从 php.ini 向输出发送错误:

    display_errors = Off
    

    这是之前给出的响应: Stop printing php error messages to browser

    【讨论】:

    • 这应该是公认的答案,因为这是对原始问题的一般答案。当然,修复错误的真正原因很重要,但这不是重点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多