![](/img/trans.png)
[英]Ajax call within WordPress returning entire HTML page in response
[英]Ajax GET returning entire page or just PHP response
從幾天開始,我試圖使這段代碼起作用:
.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
您畢竟需要打印json字符串
采用
echo json_encode($info);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.