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 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题