我的 PHP:
if($count == 1 && $user_level < 4 && $if_active == 1)
{
$response = array("success" => true);
//The login stuff happens here
echo json_encode($response);
}
elseif($count == 1 && $user_level < 4 && $if_active == 0)
{
$response = array("success" => not_act);
echo json_encode($response);
}
else
{
$response = array("success" => false);
echo json_encode($response);
}
从该 PHP 文件发送和接收数据的 JQuery
$('#login_form').submit(function(){
var emailB = encodeURIComponent($('#email_B').val());
var passwordB = encodeURIComponent($('#password_B').val());
$('#wrapper').fadeOut();
$('#login_effect').fadeIn();
$.ajax({
type: 'POST', url: 'checklogin.php', dataType: "json", data: { email: emailB, password: passwordB, },
success: function(result) {
if (!result.success) { timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); $('#wrong_password').fadeIn(); }
else if (result.success == not_act) {timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); $('#not_act').fadeIn();}
else { timeout = setTimeout(function(){ $('#login_effect').fadeOut(); }, 1500); document.location.href='http://matchmingle.net/profile/'; }
}
});
return false;
});
除了未激活的部分之外,这工作得很好而且很花哨。除了 true 和 false 之外,如何在 JQuery 中找到 JSON 返回的内容?我尝试过
$response = array("success" => not_act);
然后在JQUery中
else if (result.success == not_act) {do cool stuff}
但我无法让它工作,因为我显然做错了。顺便说一句,我对 JQuery/JSON/Javascript 很陌生,你看到的我所做的就是我在几周内从一本傻瓜书中学到的东西以及你们的帮助。
所以,是的,设置 json 响应变量并找出它在 JQuery 中的值的正确方法是什么? 非常感谢 -迈克
最佳答案
我认为你需要添加引号
在你的PHP中:
$response = array("success" => "not_act");
在你的 JavaScript 中:
else if (result.success == "not_act") {/*do cool stuff*/}
因为 not_act 似乎是一个变量名,我不知道这个变量在你的 php 代码和 javascript 代码中是否具有相同的值。
通过用引号括起来,它现在将是一个字符串。
关于php - 通过 JSON、JQuery、PHP 传递特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334293/