A
A
Andrey Elsukov2020-05-25 11:05:56
PHP
Andrey Elsukov, 2020-05-25 11:05:56

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);
        },
    });
});


Here is the php code:
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]);
                }
            }
        }


Here is the error:
VM9753:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse ()
at Object.success (VM9709 let.js:36)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest. (jquery.min.js:4)

I recently started learning Web Development, so please don't throw your slippers :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2020-05-25
@Dropsen

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.

A
abi7project, 2020-05-26
@abi7project

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 question

Ask a Question

731 491 924 answers to any question