长期读者,第一次海报。我对 jQuery 和 JSON 的世界还很陌生,并且发现我正在运行的登录脚本存在问题。
最终目标是从表单中捕获数据,将该数据传递给 PHP 文件以通过 jQuery.ajax() post 进行处理,将数据与 MySQL 数据库进行比较以进行身份验证,并返回成功或失败的数据.
我的问题是我无法将 JSON 格式的数据从 PHP 脚本传递回 jQuery。使用 Chrome 开发者工具查看处理过程时,我看到“登录失败”。我通过将数组 $rows 扔到我的 error_log 文件中来仔细检查它,它返回格式正确的 JSON,但我终究不能让它返回到 jQuery 文件。感谢您的帮助。
我的表单输入:
<!-- BEGIN: Login Page -->
<section data-role="page" id="login">
<header data-role="header" data-theme="b">
<a href="#landing" class="ui-btn-left">Back</a>
<h1>Please Log In</h1>
</header>
<div data-role="content" class="content" data-theme="b">
<form id="loginForm" action="services.php" method="post">
<div data-role="fieldcontain">
<label for="schoolID">School ID</label>
<input type="text" name="schoolID" id="schoolID" value="" />
<label for="userName">Username</label>
<input type="text" name="userName" id="userName" value="" />
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" />
<h3 id="notification"></h3>
<button data-theme="b" id="submit" type="submit">Submit</button>
<input type="hidden" name="action" value="loginForm" id="action">
</div>
</form>
</div>
<footer data-role="footer" data-position="fixed" data-theme="b">
<h1>Footer</h1>
</div>
</section>
<!-- END: Login Page -->
我的 jQuery 处理程序:
// Listen for the the submit button is clicked, serialize the data and send it off
$('#submit').click(function(){
var data = $("#loginForm :input").serializeArray();
var url = $("#loginForm").attr('action');
$.ajax({
type: 'POST',
url: url,
cache: false,
data: data,
dataType: 'json',
success: function(data){
$.ajax({
type: 'GET',
url: "services.php",
success: function(json){
alert(json);
$('#notification').append(json);
}
});
}
});
});
这是我的 PHP 处理:
if (isset($_POST['action'])) {
$schoolID = $_POST['schoolID'];
$userName = $_POST['userName'];
$password = $_POST['password'];
$sql = "SELECT FirstName, LastName, FamilyID, StudentID, UserID ";
$sql .= "FROM Users ";
$sql .= "WHERE SchoolID = '$schoolID' ";
$sql .= "AND Username = '$userName' ";
$sql .= "AND Password = '$password'";
$rs = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$row_array['firstName'] = $row['FirstName'];
$row_array['lastName'] = $row['LastName'];
$row_array['familyID'] = $row['FamilyID'];
$row_array['studentID'] = $row['StudentID'];
$row_array['userID'] = $row['UserID'];
array_push($rows, $row_array);
}
header("Content-type: application/json", true);
echo json_encode(array('rows'=>$rows));
exit;
}else{
echo "Login Failure";
}
最佳答案
看看你犯了什么错误,很简单
$.ajax({
type: 'POST',
url: 'services.php',
cache: false,
data: $('#loginForm').serialize(),
dataType: 'json',
success: function(data){
alert(data);
$('#notification').append(data);
}
});
});
使用jquery的序列化功能。当你在成功函数中有一个参数时,它不是你在这条指令中的“数据”
data : data,
它是从 php 端返回的,它是成功返回的新数据。为避免冲突,请使用其他类似 new_data 的东西
success: function(new_data){
alert(new_data);
$('#notification').append(new_data);
}
新数据是json格式的,检查一下。如果您使用的是 firefox,请使用 console.log 在 firebug 中查看。
success: function(new_data){
console.log(new_data);
alert(new_data);
$('#notification').append(new_data);
}
关于php - 使用 jQuery 从 PHP 文件返回 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9201845/