0
我有以下代碼。當我使用api客戶端發佈帖子時,它給出了成功完整的json輸出。在jQuery中顯示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"
}
]
}
感謝您的答覆男人,但我很警覺,它仍然說未定義。如果你有任何問題,請給我示例鏈接。如果可能的話嘗試運行這個應用程序。它不工作。 –
它也顯示令牌意外..! –