这是我使用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}/$1 [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?/$1 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;
#}
}
为什么重定向不像预期的那样起作用?它返回字符串,而不是重定向url。请帮帮忙!
发布于 2015-05-08 02:58:32
AJAX的设计并不是为了这样的工作。你用代码重定向
redirect('homepage'); #REDIRECT
是将ajax请求重定向到该页面,该页面然后从主页视图中获取信息,然后将其作为AJAX响应传递回来。
如果要重定向,则应通过AJAX传回一条成功或失败的消息,然后使用JavaScript在浏览器内重定向。
https://stackoverflow.com/questions/30121915
复制