我有一个使用getJSON和PHP的登录。JQuery getJSON/PHP返回数据并显示数据
现在它所做的只是检查用户和密码是否有效,如果是,我想将更多信息传递给客户端。
现在在服务器端我这样做:
if ($result->num_rows) {
echo '{"response":{"error": "1"}}';
} else {
echo '{"response":{"error": "0"}}';
}
,并在客户端上我有这样的:
if (json.response.error == "1") {
data = "Welcome "+username;
alert(data);
} else {
// Login fail
data = "Wrong Username/Password";
alert(data);
}
所以基本上它只是检查。
如何将更多信息从服务器传递到客户端?
更新:我已经改变了代码,这一点,但它现在不工作:
服务器:
if ($result->num_rows) {
//echo '{"response":{"error": "1"}}';
$data = array(
'response' => array(
'error' => 1,
),
'someData' => 'data';
);
print json_encode($data);
}
客户:
$.getJSON("server.php", {username:username,password:password},function(data)
{
alert('hello');
var json = JSON.parse(data);
alert(json.responseCode);
});
我已经添加了它,但它不再记录:echo'{“response”:{“error”:“1”}}'; $ data = array( 'responseCode'=> 1, 'someData'=>'data'; ); print json_encode($ data); – Satch3000
我编辑了回复,但你也需要删除你的'回声'。 –
我已经删除了服务器代码的回声,客户端看起来像这样: – Satch3000