[英]How to find whether it is Ajax request or not in codeigniter?
I have login through ajax to my application. 我已通过ajax登录到我的应用程序。 When user before login, try access admin home page(
http://localhost/ci3/admin/Adminhome
) that time it redirects him to admin login page( http://localhost/ci3/admin/Adminlogin
). 当用户在登录前,尝试访问管理主页(
http://localhost/ci3/admin/Adminhome
),然后将其重定向到管理员登录页面( http://localhost/ci3/admin/Adminlogin
)。 After login as user and try to access admin home page, he will get success. 以用户身份登录并尝试访问管理主页后,他将获得成功。 Now I want to find ajax request.
现在我想找到ajax请求。 I have tried something like below,
我试过类似下面的东西,
<?php
class Admin_controller extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$this->load->library('user_agent');
$adminId = $this->session->userdata('cp_adminid');
if($adminId == null){
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
echo "it is ajax request";
} else {
echo "it is not ajax request";
}
} else {
}
}
?>
$('#adminloginform').validate({
errorClass: 'error',
validClass: 'valid',
errorPlacement: function(error, element) {
error.insertAfter(element);
},
rules: {
username:{
required: true,
},
password:{
required: true,
minlength: 6,
}
},
messages:{
username: {
required: "Email is required",
},
password: {
required: "Password is required",
minlength: "Atleast 6 characters",
}
},
submitHandler: function(){
var admin=$('#adminloginform').serializeArray();
$.post("<?php echo base_url()?>admin/Adminlogin/auth",admin,function(data){
if((data.result == 1) && (data.row.view == 1 || data.row.edit == 1 || data.row.add == 1 || data.row.deleteRole == 1 )){
window.location="<?php echo base_url()?>admin/Adminhome";
} else if (data.result == 1) {
window.location="<?php echo base_url()?>admin/Userhomepage";
} else {
noty({ text: 'Username or Password is incorrect',type: 'error',modal:true,timeout:1000});
}
//$(".simplemodal-close").trigger("click");
},"json");
}
});
If you are using it within the application than why not to create a flag. 如果您在应用程序中使用它而不是为什么不创建标志。 When you call from ajax set it to true and by-default make it false.
当您从ajax调用时将其设置为true,并且默认情况下将其设置为false。
class Admin_controller extends CI_Controller{
function __construct($AjaxFlag = false)
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$this->load->library('user_agent');
$adminId = $this->session->userdata('cp_adminid');
if($adminId == null){
if($AjaxFlag){
echo "it is ajax request";
} else {
echo "it is not ajax request";
}
} else {
}
}
Check in controller function
function function_name()
{
if(is_ajax_request()){
}
}
Use helper to check this ajax request
function is_ajax_request()
{
$CI = & get_instance();
if(!$CI->input->is_ajax_request()) {
exit('No direct script access allowed');
}
else
{
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.