0

我有以下代码。当我使用 api 客户端发布帖子时,它会成功输出完整的 json。

但是当我使用 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"
        }
    ]
}
4

1 回答 1

0

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

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

于 2013-11-14T18:46:58.230 回答