【发布时间】: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 响应。