在jquery中由php返回的json中显示解析错误

时间:2013-11-14 18:33:34

标签: php jquery json post

我有以下代码。当我使用api客户端发帖时,它会成功完成json out。

但是当我使用jQuery时,它会抛出一个json解析错误。

我想知道如何在jQuery中操作json数据和对象。我想做一个安静的api。所以请帮助我。

这是我的HTML标记:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Sign in</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="js/app.js" rel="javascrpt">
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <script type="text/javascript" src="js/jquery-2.0.3.min.js" />
    <script type="text/javascript" src="js/app.js" />
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="js/html5shiv.js"></script>
    <![endif]-->

    <script>


    </script>
    <!-- Fav and touch icons -->
<script type="text/javascript">
    $(document).ready(function(){

        $("#submit").on('click',function(){
                 console.log($('#yourform').serialize());
                 $.ajax({
            type: 'post',
            url : 'auth.php',
            data : $('#yourform').serialize(),
            success : function(data){
            alert(data.username);
                if (data.username == "a")
                {
                $('#error').html('sc');
                }
                else{
                $('#error').html('Email/Password is wrong.');
                }
            }
          });
        }); 
    });

</script>
  </head>

  <body>

    <div class="container">

      <form class="form-signin" id="yourform">
        <h2 class="form-signin-heading">Please sign in</h2>
        <input type="text" id ="email" name='email' class="input-block-level" placeholder="Email address" required>
        <input type="password" id="pwd" name='pwd' class="input-block-level" placeholder="Password" required>
        <div class="error">
            <a href="#">Forgot Password?</a>
            <p id="error">Email/Password is wrong.</p>
        </div>
        <input type="button" id="submit" class="btn btn-large btn-primary" value="Sign in"/>
      </form>

    </div> <!-- /container -->

    <!-- Le javascript
    ================================================== -->

  </body>
</html>

我的PHP代码:

 <?php

$email=$_POST['email'];
$pwd=$_POST['pwd'];

header('Access-Control-Allow-Origin: *');
$jsonString = file_get_contents(dirname(__FILE__) . "/db/user.json");
$data = json_decode($jsonString);


foreach ( $data->user as $user )
{
    if( $user->email == $email && $user->password ==$pwd )
    {
        //header('Content-Type: application/json');         
        echo($user);

    }
    else
    {
        echo"Fail";
    }
} 
?>

我的json文件

{
    "user": [
        {
            "username": "hiren",
            "email": "a",
            "password": "a",
            "que": "Favourite Pet",
            "ans": "Montu"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

data已经是一个对象,不需要使用$.parseJSON(data)(它会导致错误,因为$.parseJSON需要一个字符串而不是一个对象作为参数)

还有什么:您不应该返回$ data(它包含所有用户的登录数据),而是返回$user。 如果成功,请使用alert(h.username);