dqnek0079 2019-03-09 16:37
浏览 104
已采纳

某些部署会返回以下错误:未捕获的SyntaxError:位于0的JSON中的意外的令牌<

I have a login form, which has to be executed through ajax to PHP. From PHP he has to send a message back to ajax, and Ajax has to display the message. The problem is, that some of the messages doesn't display. It'll give the following error: Uncaught SyntaxError: Unexpected token < in JSON at position 0

Conditional Statements:

As I said, some statements does work and some doesn't

  • Check if form is empty. This doesn't work. What I've tried: I opened the login.php via URL. So, the form was empty. It returned: {"msg":"formleeg"} as it has to be. But still, it doesn't work.
  • If the form isn't empty: Check if the username is correct. This doesn't work.
  • Check if the password is correct. This works correctly.
  • Last statement, the else works perfect.

My Codes:

The form:

<form id="loginform" class="col s12" name="loginform" method="post">
    <div class="input-field col s12">
        <i class="fas fa-user material-icons prefix"></i>
        <input id="hn" type="text" class="validate" name="hn">
        <label for="hn">Gebruikersnaam</label>
    </div>
    <div class="input-field col s12">
        <i class="fas fa-key material-icons prefix"></i>
        <input id="ww" type="password" class="validate" name="ww">
        <label for="ww">Wachtwoord</label>
    </div>
    <button class="mui-btn mui-btn--raised mui-btn--primary" id="forminlog"><i class="fas fa-chevron-right"></i> Inloggen</button>
</form>

Ajax:

$("#forminlog").click(function(){
    $.ajax({
        type: 'POST',
        data: {hn: document.getElementById("hn").value, ww: document.getElementById("ww").value},
        url: 'login.php',
        success: function(antwoordform) {
            var jsonverzoek = JSON.parse(antwoordform);
            if(jsonverzoek.msg == "formleeg"){
                swal("Lege form")
            }
            else if(jsonverzoek.success == true){
                swal("Everything OK");
            }
            else {
                swal("false");
            }
        }
    });
    event.preventDefault()
});

PHP:

<?php
$resultaat = [];
function login(){
    //VARIABLEN
    $pgebruiker = $_POST['hn'];
    $pww = $_POST['ww'];
    //LEGE VELDEN
    if(empty($pgebruiker) || empty($pww)){
        echo 'Vul alle velden in';
        $resultaat["msg"] = "formleeg";
    }
    else {
        //DATA VANUIT DB
        include("connection.php");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sth = $dbh->prepare("SELECT * FROM gebruikers WHERE gebruikersnaam = :gebruiker");
        $pdoExec = $sth->execute(array(":gebruiker"=>$pgebruiker));
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_OBJ);
        //GEBRUIKERSNAAM
        $gebruikersnaam = $result->gebruikersnaam;
        //WACHTWOORD
        $wachtwoord =  $result->wachtwoord;
        if(!strcasecmp($pgebruiker, $gebruikersnaam) == 0){
            $resultaat["success"] = false;
        }
        elseif(!password_verify($pww, $wachtwoord) /*strcasecmp($pww, $wachtwoord) == 0*/){
            $resultaat["success"] = false;
        }
        else{
            $resultaat["success"] = true;
            /*session_start();
            $_SESSION['gebruiker'] = $gebruikersnaam;
            header('Location: veilig.php');*/
            //exit;
        }
    }
    echo json_encode($resultaat);
}
login();
?>
  • 写回答

1条回答 默认 最新

  • dongyi1524 2019-03-10 16:37
    关注

    I found it. 2 problems:

    • There was a echo inside the code.
    • I don't know exactly the second problem but I added ob_start in my login.php.

    So the code will be:

    <?php
    ob_start();
    $resultaat = [];
    function login(){
        //VARIABLEN
        $pgebruiker = $_POST['hn'];
        $pww = $_POST['ww'];
        //LEGE VELDEN
        if(empty($pgebruiker) || empty($pww)){
            $resultaat["msg"] = "formleeg";
        }
        else {
            //DATA VANUIT DB
            include("connection.php");
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sth = $dbh->prepare("SELECT * FROM gebruikers WHERE gebruikersnaam = :gebruiker");
            $pdoExec = $sth->execute(array(":gebruiker"=>$pgebruiker));
            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_OBJ);
            //GEBRUIKERSNAAM
            $gebruikersnaam = $result->gebruikersnaam;
            //WACHTWOORD
            $wachtwoord =  $result->wachtwoord;
            if(!strcasecmp($pgebruiker, $gebruikersnaam) == 0){
                $resultaat["success"] = false;
            }
            elseif(!password_verify($pww, $wachtwoord) /*strcasecmp($pww, $wachtwoord) == 0*/){
                $resultaat["success"] = false;
            }
            else{
               $resultaat["success"] = true;
               /*session_start();
                $_SESSION['gebruiker'] = $gebruikersnaam;
                header('Location: veilig.php');*/
                //exit;
            }
        }
        ob_end_clean();
        echo json_encode($resultaat);
    }
    login();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c