Codeigniter 重定向 returns 字符串而不是将其传递给 url
Codeigniter redirect returns string instead of passing it to the url
这是我使用 ajax 登录的控制器。
Login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index(){
$this->load->view('login');
}
public function checkLogin(){
if($this->input->is_ajax_request() && isset($_POST['email']) && isset($_POST['password'])){#restrict direct access from browser
if(empty($this->input->post('email')) || empty($this->input->post('password'))){
echo "not";
}else{
if(filter_var($this->input->post('email'), FILTER_VALIDATE_EMAIL) === false){
echo "not";
}else{
$data = array(
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password'))
);
if($this->db->select('*')&&
$this->db->from('users')&&
$this->db->where($data)){
if($this->db->count_all_results() > 0){
redirect('homepage'); #REDIRECT
}else{
echo "not";
}
}
}
}
}else{
show_error("No direct script access allowed");
}
}
}
这是我的 javascript 文件:
$(document).ready(function(){
$("#login-form").submit(function(event){
event.preventDefault();
$.ajax({
url: 'http://citest.local/login/checkLogin',
type: 'POST',
data: {
email: $.trim($("#email").val()),
password: $.trim($("#password").val())},
})
.done(function(result) {
if(result == "not"){
$(".has-feedback").addClass('has-error');
$(".glyphicon").addClass('glyphicon-remove');
}else{
$(".has-feedback").removeClass('has-error');
$(".glyphicon").removeClass('glyphicon-remove');
$(".has-feedback").addClass('has-success');
$(".glyphicon").addClass('glyphicon-ok');
console.log(result);
}
});
});
});
这是我控制器的重定向功能返回到控制台的结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Simple Contacts</title>
<link rel="stylesheet" href="">
</head>
<body>
</body>
</html>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Disable rewrite for valid directory/files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#map all request urls to a specific controller method
RewriteRule ^(.*)$ index.php?/{controller}/{method}/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
站点配置 (nginx)
server {
listen 80;
root /home/user/trytry/ci_test;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name citest.local;
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/ last;
break;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
为什么重定向不能正常工作?它 returns 字符串而不是重定向 url。请帮忙! :(
AJAX 不是为那样工作而设计的。您的重定向代码
redirect('homepage'); #REDIRECT
将您的 ajax 请求重定向到该页面,然后该页面从主页视图获取信息,然后将其作为 AJAX 响应传回。
如果你想重定向,你应该通过AJAX传回一个成功或失败的消息,然后使用JavaScript在浏览器中重定向
这是我使用 ajax 登录的控制器。 Login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index(){
$this->load->view('login');
}
public function checkLogin(){
if($this->input->is_ajax_request() && isset($_POST['email']) && isset($_POST['password'])){#restrict direct access from browser
if(empty($this->input->post('email')) || empty($this->input->post('password'))){
echo "not";
}else{
if(filter_var($this->input->post('email'), FILTER_VALIDATE_EMAIL) === false){
echo "not";
}else{
$data = array(
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password'))
);
if($this->db->select('*')&&
$this->db->from('users')&&
$this->db->where($data)){
if($this->db->count_all_results() > 0){
redirect('homepage'); #REDIRECT
}else{
echo "not";
}
}
}
}
}else{
show_error("No direct script access allowed");
}
}
}
这是我的 javascript 文件:
$(document).ready(function(){
$("#login-form").submit(function(event){
event.preventDefault();
$.ajax({
url: 'http://citest.local/login/checkLogin',
type: 'POST',
data: {
email: $.trim($("#email").val()),
password: $.trim($("#password").val())},
})
.done(function(result) {
if(result == "not"){
$(".has-feedback").addClass('has-error');
$(".glyphicon").addClass('glyphicon-remove');
}else{
$(".has-feedback").removeClass('has-error');
$(".glyphicon").removeClass('glyphicon-remove');
$(".has-feedback").addClass('has-success');
$(".glyphicon").addClass('glyphicon-ok');
console.log(result);
}
});
});
});
这是我控制器的重定向功能返回到控制台的结果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Simple Contacts</title>
<link rel="stylesheet" href="">
</head>
<body>
</body>
</html>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Disable rewrite for valid directory/files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#map all request urls to a specific controller method
RewriteRule ^(.*)$ index.php?/{controller}/{method}/ [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
站点配置 (nginx)
server {
listen 80;
root /home/user/trytry/ci_test;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name citest.local;
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/ last;
break;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
为什么重定向不能正常工作?它 returns 字符串而不是重定向 url。请帮忙! :(
AJAX 不是为那样工作而设计的。您的重定向代码
redirect('homepage'); #REDIRECT
将您的 ajax 请求重定向到该页面,然后该页面从主页视图获取信息,然后将其作为 AJAX 响应传回。
如果你想重定向,你应该通过AJAX传回一个成功或失败的消息,然后使用JavaScript在浏览器中重定向