Answer the question
In order to leave comments, you need to log in
Ajax request not working, what should I do?
Actually the problem is that I began to re-pick my site so that everything interacts using json. And everything worked out for me until I got to the point where I needed to find out the user's login. There are files in the scripts folder:
get_user.php - to get the user object
<?php
include "connect.php";
//Получаем все поля пользователя с таким логином
session_start();
$id=$_SESSION["id"];
$result = $mysqli->query("SELECT * FROM users WHERE id='$id'");
//Преобразуем в массив
$result = mysqli_fetch_assoc($result);
echo json_encode($result);
?>
include "functions.php";
$response=array()
session_start();
if (isAuth()) {
$response["response"]="true";
echo json_encode($response);
} else {
$response["response"]="false";
echo json_encode($response);
}
function isAuth() {
$.ajax({
type:"post",
url:"scripts/is_auth.php",
success:function(data,text) {
var res=JSON.parse(data);
alert(res.response);
if (res.response=="true") {
return true;
} else {
return false;
}
}
})
}
function getUser() {
$.ajax({
type:"post",
url:"scripts/get_user.php",
success:function(data,text) {
var response=JSON.parse(data);
alert(response);
return response;
}
})
}
Answer the question
In order to leave comments, you need to log in
In order for Ajax requests to work with sessions, you need to include xhrFields: {
withCredentials: true
} in the request parameters
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question