gpt4 book ai didi

javascript - 重定向在某些 Controller 中有效,而在某些 Controller 中无效(相同代码) php 以及 javascript

转载 作者:行者123 更新时间:2023-12-03 08:21:54 24 4
gpt4 key购买 nike

我快要疯了,几天来试图弄清楚为什么重定向有效在 php 中,也在 javascript 中,在某些 Controller 中,有些不在 Controller 中它们是相同的代码和相同的 View html 代码同样的 Ajax 相同的形式我尝试使用 location href 和 php 重定向它不工作函数中的所有方法都起作用,只有重定向或 json 编码不起作用在 Firebug 的控制台中,它显示“帖子”发生了显示错误 TypeError: data is null

顺便说一句,只有当我输入正确的用户名和密码时才会发生这种情况当我写错信息时出现错误消息用户名或密码无效并在控制台中{“st”:0,“msg”:“无效的用户名或密码”}

Controller 不工作

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class signin_admin extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model');
}

public function index() {
if ($this->session->userdata('logged_in')) {
redirect('home');
} else {
$this->load->view('signin_admin_view');
}
}

public function do_login() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[4]|max_length[20]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[4]|max_length[20]');

if ($this->form_validation->run() == FALSE) {
$output = array('st' => 0, 'msg' => validation_errors());
echo json_encode($output);
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->admin_model->login($data);
if ($result == TRUE) {

$username = $this->input->post('username');
$result = $this->admin_model->read_user_information($username);
if ($result != false) {
$session_data = array('user_id' => $result[0]->user_id,);
$this->session->set_userdata('logged_in_admin', $session_data);
$output = array('st' => 1);
echo json_encode($output);
}
} else {
$output = array('st' => 0, 'msg' => "Invalid Username or Password");

echo json_encode($output);
}
}
}

}

工作 Controller

<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class signin extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
}

public function index() {
if ($this->session->userdata('logged_in')) {
redirect('home');
} else {
$this->load->view('signin_view');
}
}

public function do_login() {

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[4]|max_length[20]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[4]|max_length[20]');

if ($this->form_validation->run() == FALSE) {
$output = array('st' => 0, 'msg' => validation_errors());
echo json_encode($output);
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->user_model->login($data);
if ($result == TRUE) {

$username = $this->input->post('username');
$result = $this->user_model->read_user_information($username);
if ($result != false) {
$session_data = array('user_id' => $result[0]->user_id, 'username' => $result[0]->username);
$this->session->set_userdata('logged_in', $session_data);
$output = array('st' => 1);
echo json_encode($output);

}
} else {
$output = array('st' => 0, 'msg' => "Invalid Username or Password");

echo json_encode($output);
}
}
}

}

工作 Controller 和不工作 Controller 的ajax,不同的是location.href

<script src="public/js/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(event){
$.post(
$('#frm').attr('action'),
$('#frm').serialize(),
function(data)
{

if (data.st == 0)
{
$('#validation-error').html(data.msg);
}
else if (data.st == 1)
{
window.location.href = "<?php echo site_url('home'); ?>";
}
},
'json'
);
return false;
});


});

</script>

管理员模型(不工作重定向)

public function login($data) {

$condition = "username =" . "'" . $data['username'] . "' AND " . "password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('admins');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}

读取用户模型中的用户信息函数(工作中)

public function read_user_information_profile($user_id) {

$condition = "user_id =" . "'" . $user_id . "'";
$this->db->select('*');
$this->db->from('users');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}

管理模型中读取用户信息功能(不工作 Controller )

public function read_user_information($user_id) {

$condition = "user_id =" . "'" . $user_id . "'";
$this->db->select('*');
$this->db->from('admins');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();

if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}

最佳答案

您尚未显示 user_model->read_user_information($username) 的代码;

但是在看到你的 Controller 代码后,当read_user_information($username)返回false时,没有错误的回显。这就是 TypeError: data is null 的可能原因。

要找到确切的错误,你必须修改 Controller 和jquery,

Controller 中修改后的 do_login() fn:

public function do_login() {

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[4]|max_length[20]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[4]|max_length[20]');

if ($this->form_validation->run() == FALSE) {
$output = array('st' => 0, 'msg' => validation_errors());
echo json_encode($output);
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->user_model->login($data);
if ($result == TRUE) {

$username = $this->input->post('username');
$result = $this->user_model->read_user_information($username);
if ($result != false) {
$session_data = array('user_id' => $result[0]->user_id, 'username' => $result[0]->username);
$this->session->set_userdata('logged_in', $session_data);
$output = array('st' => 1);
echo json_encode($output);

} else {
$output = array('st' => 0, 'msg' => "Failed: read_user_information");
echo json_encode($output);
}
} else {
$output = array('st' => 0, 'msg' => "Invalid Username or Password");
echo json_encode($output);
}
}
}

和 Jquery:

<script src="public/js/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(event){
$.post(
$('#frm').attr('action'),
$('#frm').serialize(),
function(retdata)
{
// console.log(retdata);
data = JSON.parse(retdata);
if (data.st == 0)
{
$('#validation-error').html(data.msg);
}
else if (data.st == 1)
{
window.location.href = "<?php echo site_url('home'); ?>";
}
}
);
return false;
});
});

</script>

如果出现任何错误,只需取消注释 console.log(retdata);从jquery查看真实的输出,你就可以找出问题所在。

关于javascript - 重定向在某些 Controller 中有效,而在某些 Controller 中无效(相同代码) php 以及 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33708248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com