Answer the question
In order to leave comments, you need to log in
Json_encode and JSON.parse() how to solve syntax error?
There is a js code that catches a button click and ajax sends it to the executable file. The executing file receives data, checks for the presence of data from the database and sends a request back.
And so, when I send back json, then writes an error. I don't know how to solve it. Need your help)
Actually, the code itself:
$(".enter").on("click", function() {
event.preventDefault();
var user = $('.user').val();
var password = $('.password').val();
$.ajax({
url: '../registr/reg.php',
type: 'POST',
data: {
'user': user,
'password': password,
'enter': true,
},
success: function(data) {
let user = JSON.parse(data);
},
});
});
if(isset($data['enter'])){
$us = text_in($_POST['user']);
$password = text_in(md5(md5(trim($_POST['password']))));
$stn = $pdo->prepare("SELECT * FROM users WHERE user_login=:user");
$stn-> execute(array(":user"=>$us));
if (!empty($stn->fetch(PDO::FETCH_ASSOC)))
{
$stn = $pdo->prepare("SELECT * FROM users WHERE user_password = :password");
$stn-> execute(array(":password"=>$password));
if (!empty($stn->fetch(PDO::FETCH_ASSOC))){
$usr = $pdo->prepare("SELECT user_id FROM users WHERE user_login=:user");
$nick = $pdo->prepare("SELECT user_login FROM users WHERE user_login=:user");
$nick ->execute(array(":user"=>$us));
$name = $nick->fetch(PDO::FETCH_ASSOC);
$names = array_values($name);
$nickname = implode($names);
$usr ->execute(array(":user"=>$us));
$usi = $usr->fetch(PDO::FETCH_ASSOC);
$usid = implode($usi);
$hash = md5(generateCode($usid));
setcookie('name', $nickname, time()+3600, "/", null, null, true);
setcookie('id', $hash, time()+3600, "/", null, null, true);
echo json_encode(["nickname" => $nickname]);
}
}
}
Answer the question
In order to leave comments, you need to log in
Open the browser console, the "Network" tab and see what exactly is returned from the server in response to your request. Judging by the error, in addition to/instead of JSON you are returning HTML.
Why not specify the json request type in the request right away? Why else parse the answer?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question