Ajax GET возвращает всю страницу или только ответ PHP - PullRequest
0 голосов
/ 26 июня 2018

Через несколько дней я пытаюсь заставить этот код работать:

.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

1 Ответ

0 голосов
/ 26 июня 2018

Вам нужно распечатать строку json после всех

использовать

echo json_encode($info);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...