我正在做下一个......
来自html(jQuery)的请求
$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
$("#signin_ok").html("You sign in now "+data.name+". Your password is: "+data.pass);
$("#signin_ok").show()
.animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
.animate({fontSize:"100%"}, 100)
.animate({fontSize:"100%"}, 3000)
.animate({fontSize:"120%"}, 200, function() {
$(this).css({display:"none"});
});
});
从html(jQuery)中的php返回
if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
if(isset($_POST['username']) && isset($_POST['userpass'])) {
$username = $_POST['username'];
$userpass = $_POST['userpass'];
echo json_encode(array("name" => $username, "pass" => $userpass));
}
}
但返回“您现在登录未定义。您的密码为:未定义”
怎么了?如何解决?
P.S。
当我像这样添加“json”时
$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
. . . . . .
. . . . . .
}, "json");
没有反应
发生了什么事?
答案 0 :(得分:2)
尝试使用
将JSON标头添加到PHP脚本中header('Content-type: application/json');
// then your code
另外,似乎你只是返回传递的数据,为什么不只是使用user并传递数据,而不是data.user和data.pass?
答案 1 :(得分:0)
这可以解决这个问题吗?
$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
$("#signin_ok").html("You sign in now "+data[0].name+". Your password is: "+data[0].pass);
$("#signin_ok").show()
.animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
.animate({fontSize:"100%"}, 100)
.animate({fontSize:"100%"}, 3000)
.animate({fontSize:"120%"}, 200, function() {
$(this).css({display:"none"});
});
});
答案 2 :(得分:0)
我会回复一个$ .ajax调用,因为这往往是我使用的,因为$ .post反正包装$ .ajax ......
$.ajax({
type: "POST",
url: "index.php?res=ok",
data: { "username" : user, "userpass": pass },
success: function(msg) {
var data = $.parseJSON(msg);
alert(data.username);
alert(data.userpass);
}
});
答案 3 :(得分:0)
我自己已经解决了这个问题,我想是这样的:)
我创建了新文件(new.php):
<?php
if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
if(isset($_POST['username']) && isset($_POST['userpass'])) {
$username = $_POST['username'];
$userpass = $_POST['userpass'];
$arr = array("name" => $username, "pass" => $userpass);
echo json_encode(array("name" => $username, "pass" => $userpass));
}
}
?>
并将请求从HTML传递给PHP(new.php)
. . .
$.post("new.php", { "username": user, "userpass": pass, "res": "ok" }, function(data) {
$("#signin_ok").html("You sign in now "+data['name']+". Your password is: "+data['pass']);
console.log(data);
$("#signin_ok").show()
.animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
.animate({fontSize:"100%"}, 100)
.animate({fontSize:"100%"}, 3000)
.animate({fontSize:"120%"}, 200, function() {
$(this).css({display:"none"});
});
}, "json");
. . .
我得到了结果:) 但它是愚蠢的,为什么它在index.php中不起作用