几天来,我正试图使此代码正常工作:
.PHP:
if (($_SERVER['REQUEST_METHOD'] === 'GET') && isset($_GET['tkn']) && isset($_GET['t'])) {
$tkn = $mysqli->real_escape_string($_GET['tkn']);
$t = $mysqli->real_escape_string($_GET['t']);
if (time() < $t) {
$info["msg"]="<strong>Error! </strong>link valid!";
$info["cls"]="brand";
}
else {
$info["msg"]="<strong>Error! </strong>Link expired!";
$info["cls"]="danger";
}
$mysqli->close();
json_encode($info);
}
.JS:
$.ajax({
url: window.location.href,
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
},
dataType: "JSON"
});
如果我使用当前的PHP代码而不添加 exit(json_encode($ info)); ,它将返回整个 html 标记,而PHP代码不会返回任何文本。
如果我要添加 exit(json_encode($ info)); ,它将仅在白色背景上显示该代码。
有什么想法吗? 谢谢!
这是我的login.php格式
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//some code goes here
echo json_encode($info);
exit;
// this part is working perfectly, is getting the answer from php via ajax and display it in page(html part in running)
}
elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
//other code goes here
echo json_encode($info);
exit;
// here is displayed only the message(without html part) if I add exit;
//and if i dont add exit is displayed the message on white background on
// the top of the page(html part is working);
?>
//html part
答案 0 :(得分:0)
您毕竟需要打印json字符串
使用
echo json_encode($info);