PHP 的 'echo' 不正确
PHP's 'echo' does not work right
我需要检查函数的某些 return 值,我将其放入 while 循环中:
//should be true if an error comes up (a function returns false)
$error = false;
while ($error == false) {
if (!$this->check_date()) {
$this->log('bad date');
$error = true;
break;
}
if (!$this->check_location()) {
$this->log('bad location');
$error = true;
break;
}
if (!$this->check_abc()) {
$this->log('bad abc');
$error = true;
break;
}
//... more if's
break;
}
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
那么,问题是什么?
我的 AJAX 调用
没有得到输出
但是如果我放回声 "test";这里:
//... more if's
break;
}
echo "test";
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
我得到以下回复:
那么,这是怎么回事?
AJAX代码:
$.ajax({
url: "index.php",
type: "POST",
async: false,
data: "do=this",
success: function (answer) {
console.log(answer);
},
error: function (jXHR, textStatus, errorThrown) {
console.log("error" + errorThrown);
}
});
感谢您的帮助:)
SOLUTION 似乎 PHP 误解了 "true" - 所以我们需要对它进行编码,例如作为 JSON 字符串:
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo json_encode("true");
} else {
$this->log('-There is an error-');
}
您响应了一个 AJAX 请求,因此您应该使用 json_encode
以确保 return 一个有效的 json。
<?php
echo json_encode('true');
在你的情况下它只是一个字符串,但是 PHP 正在做一种转换,所以使用 json_encode.
更安全
我需要检查函数的某些 return 值,我将其放入 while 循环中:
//should be true if an error comes up (a function returns false)
$error = false;
while ($error == false) {
if (!$this->check_date()) {
$this->log('bad date');
$error = true;
break;
}
if (!$this->check_location()) {
$this->log('bad location');
$error = true;
break;
}
if (!$this->check_abc()) {
$this->log('bad abc');
$error = true;
break;
}
//... more if's
break;
}
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
那么,问题是什么?
我的 AJAX 调用
没有得到输出但是如果我放回声 "test";这里:
//... more if's
break;
}
echo "test";
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo "true";
} else {
$this->log('-There is an error-');
}
我得到以下回复:
那么,这是怎么回事?
AJAX代码:
$.ajax({
url: "index.php",
type: "POST",
async: false,
data: "do=this",
success: function (answer) {
console.log(answer);
},
error: function (jXHR, textStatus, errorThrown) {
console.log("error" + errorThrown);
}
});
感谢您的帮助:)
SOLUTION 似乎 PHP 误解了 "true" - 所以我们需要对它进行编码,例如作为 JSON 字符串:
//No error - great
if ($error == false) {
//Answer to my AJAX call
echo json_encode("true");
} else {
$this->log('-There is an error-');
}
您响应了一个 AJAX 请求,因此您应该使用 json_encode
以确保 return 一个有效的 json。
<?php
echo json_encode('true');
在你的情况下它只是一个字符串,但是 PHP 正在做一种转换,所以使用 json_encode.
更安全