简体   繁体   中英

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
    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. On printing info in console I get 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". I have searched a lot on StackOverflow I found that it says there is error in either parsing JSON or in decoding it.

It looks like you are echoing "Connection success" in init.php although we can't see that.

A json response should have no other content in response ... just the json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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