-- Ajax 代码
var User = function(){
return {
init : function(){
document.getElementById('login').addEventListener('click', this.login);
},
login : function(){
var username = $("#username").val(),
password = $("#password").val();
$.ajax({
url : 'http://localhost/oc2/user_info/login',
method : 'post',
dataType : 'json',
data : {
username : username,
password : password
},
success : function(response){
alert('h'); <-- add the php return value id here.
window.location.href = "main.html";
},
error : function(response){
alert(response.responseText);
}
});
}
};
}();
-- PHP 代码点火器
public function login()
{
$post = $this->input->post();
$where = array(
'email_address' => $post['username'], //"turbobpo.johnrey@gmail.com",
'password' => md5($post['password']) //"e10adc3949ba59abbe56e057f20f883e"
);
$user_info = $this->get_by($where);
if(isset($user_info['id']))
{
$this->session->set_userdata('user_info', $user_info);
$response = array(
'id' => $user_info['id'], <-- this i want to pass to my ajax
'success' => TRUE
);
}
else
{
$response = array(
'success' => FALSE
);
}
print json_encode($response);
}
你好,你能帮助我完成这一部分吗?我已经在这个 php ajax 上走得很远了,我没有用于创建这个应用程序,我需要帮助,我在代码上发表评论,看看我想从哪里检索值php 到我的 ajax 代码中,这样我就可以在下次检索文件时使用它,我使用登录用户的 id 以网格形式获取他的可用访问权限。如果您还可以向我展示如何使用该数据在成功的 ajax 返回数组值上检索它后再次将其传递回 php 中的 id,那将是一个优势。
最佳答案
var User = function(){
return {
init : function(){
document.getElementById('login').addEventListener('click', this.login);
},
login : function(){
var username = $("#username").val(),
password = $("#password").val();
$.ajax({
url : 'http://localhost/oc2/user_info/login',
method : 'post',
dataType : 'json',
data : {
username : username,
password : password
},
success : function(response){
response.id; // Here is the id JQuery parses JSON for you
window.location.href = "main.html";
},
error : function(response){
alert(response.responseText);
}
});
}
};
}();
关于javascript - 如何获取php到ajax的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234732/