简体   繁体   English

未捕获到的SyntaxError:JSON中位置0处的意外令牌C

[英]Uncaught SyntaxError: Unexpected token C in JSON at position 0

This is my code for a form. 这是我的表单代码。 I am asking user to input email and password and checking if user is registered or not. 我要求用户输入电子邮件和密码,并检查用户是否注册。 If he is registered then I alert success: 如果他已经注册,那么我会警告成功:

<form role="form" class="legacy-form" action="" method="POST" id="myform1">
  <div class="col-xs-12 col-sm-6 col-md-6 col-sm-offset-3 col-md-offset-3">
    <div class="form-group">
      <input type="email" name="email" id="loginemail" class="form-control" placeholder="Email Address" required>
    </div>
  </div>
  <div class="col-xs-12 col-sm-6 col-md-6 col-sm-offset-3 col-md-offset-3">
    <div class="form-group">
      <input type="password" name="password" id="loginpassword" class="form-control" placeholder="Password" required>
    </div>
  </div>
  <div class="row" style="padding:15px">
    <div class="col-xs-6 col-sm-3 col-md-3  col-sm-offset-3 col-md-offset-3">
      <div class="form-group">
        <input type="submit" value="Log In" class="btn btn-primary" id="loginbtn">
      </div>
    </div>
    <div class="col-xs-6 col-sm-3 col-md-3">
      <div class="form-group">
        <input type="submit" value="Cancel" class="btn btn-danger" data-dismiss="modal">
      </div>
    </div>
  </div>
</form>

This is the php code wherein the connection is placed in another file 'init.php' 这是php代码,其中,连接位于另一个文件“ init.php”中

<?php
    include('init.php');
    if(isset($_POST))
    {
        $loginemail=$_POST["loginemail"];
        $loginpassword=$_POST["loginpassword"];
        $sql = "select count(*),fname from users where password='$loginpassword' and email='$loginemail'";
        $result=mysqli_query($con,$sql);
        if($result) {
            $response =array();

            while($row=mysqli_fetch_array($result))
            {
                array_push($response,array("Count"=>$row[0],"name"=>$row[1]));
            }
            echo json_encode(array("server_response"=>$response));
        } else {
            echo "error";
        }
        mysqli_close($con);
    }
?>

this is my js file. 这是我的js文件。 On printing info in console I get Connection sucess{"server_response":[{"Count":"1","name":"sagar"}]} 在控制台上打印信息时,我得到Connection sucess{"server_response":[{"Count":"1","name":"sagar"}]}

$("#loginbtn").click(function(e) {
  var loginemail = $("#loginemail").val();
  var loginpassword = $("#loginpassword").val();
  check_for_user(loginemail, loginpassword);

  function check_for_user(loginemail, loginpassword) {
    console.log("in check_for_user");
    var i = 0;
    console.log(i);
    i++;
    var c = "";
    var x = "1";
    var user = "";
    var formdata = {
      loginemail: loginemail,
      loginpassword: loginpassword
    }
    $.ajax({
      url: 'getData.php',
      type: "POST",
      data: formdata,
      dataType: 'text',
      success: handle_success,
      error: handle_error
    });

    function handle_success(info) {
      console.log(info);
      var obj = jQuery.parseJSON(info);
      console.log(obj);

      $(obj.server_response).each(info, function(index, value) {
        user = value.name;
        c = value.Count;
      });
      console.log(c);
      console.log(user);
      if (x == c) {
        alert("Welcome");
        //document.location='online.html';
      } else {
        alert("Enter valid username and password");
      }

    }

    function handle_error() {
      alert("error");
    }
  }
});

The flow of the code is like when the #loginbtn is clicked it posts loginemail and loginpassword on php and then it checks in database whether there is an identical entry in database, if yes it alerts "welcome". 代码的流程就像单击#loginbtn时一样,它在php上发布loginemail和loginpassword,然后在数据库中检查数据库中是否存在相同的条目,如果是,则提示“欢迎”。 I have searched a lot on StackOverflow I found that it says there is error in either parsing JSON or in decoding it. 我在StackOverflow上进行了很多搜索,发现它表示解析JSON或解码时出错。

It looks like you are echoing "Connection success" in init.php although we can't see that. 似乎您在init.php中回显"Connection success" ,尽管我们看不到。

A json response should have no other content in response ... just the json. 一个json响应应该没有其他内容响应...仅仅是json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM