javascript - 使用 ajax 函数在 codeigniter 中重定向

标签 javascript php jquery ajax codeigniter

这是我的 Controller 功能

    public function verifyUser()    
   {
        $userName =  $_POST['email'];
        $userPassword =  $_POST['password'];
        $var=array('email'=>$userName,'password'=>$userPassword);
        $check=$this->mymodel->login_validation($var);


        //$status = array("STATUS"=>"false");
        if(count($check))
        {
            redirect('main/valid_login');

        }
        else
        {
        echo "<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>Could't Authorize to the system! Try again with valid credentials.</div>" ;
        }   
    }

这是我的ajax函数

    <script>
    function makeAjaxCall(){
    $.ajax({
    type: "post",
    url: "<?php echo site_url('main/verifyUser');?>",
    cache: false,               
    data: $('#userForm').serialize(),
     success:function(msg)
    {
        $('#show_id').html(msg);
    }
    });
   }
   </script>

这是我的表格

 <form name="userForm" id="userForm" action="">
    <div id="show_id"></div>
            <fieldset>

                <p><label for="email">E-mail address</label></p>
                <p><input type="email" id="email" placeholder="enter your email id" name="email"></p> <!-- JS because of IE support; better: placeholder="mail@address.com" -->

                <p><label for="password">Password</label></p>
                <p><input type="password" id="password" placeholder="*******" name="password" style="width: 328px;"></p> <!-- JS because of IE support; better: placeholder="password" -->

                <p><input type="button" value="Sign In" onclick="javascript:makeAjaxCall();"></p>

            </fieldset>

        </form>

一切正常,但是当我输入有效的用户名和密码时,它没有重定向到任何页面,所以请帮助我

最佳答案

将 JSON 数据作为响应发送给 ajax,并根据需要进行处理。
Controller :

public function verifyUser()  {

    $userName =  $_POST['email'];
    $userPassword =  $_POST['password'];
    $var=array('email'=>$userName,'password'=>$userPassword);
    $check=$this->mymodel->login_validation($var);


   //$status = array("STATUS"=>"false");
   if(count($check)) {
        $this->output
            ->set_content_type("application/json")
            ->set_output(json_encode(array('status'=>true, 'redirect'=>base_url('main/valid_login') )));
   }
   else {
        $this->output
            ->set_content_type("application/json")
            ->set_output(json_encode(array('status'=>false, 'error'=>'Could't Authorize to the system! Try again with valid credentials.')));
    }   
}

使用ajax处理JSON数据。

$.ajax({
    type: "post",
    url: "<?php echo site_url('main/verifyUser');?>",
    cache: false,               
    data: $('#userForm').serialize(), 
    dataType: 'json', 
    success:function(response) {
        if( response.status === true )
            document.location.href = response.redirect;
        else 
            $('#show_id').html("<div style='border:1px solid red;font-size: 11px;margin:0 auto !important;'>"+response.error+"</div>");
    }
});

关于javascript - 使用 ajax 函数在 codeigniter 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27718030/

相关文章:

php - 如何在PHP中设置文本文件编码?

javascript - angularjs无限滚动显示数据

php - 将字符串变量从 php 传递到 JS 函数

javascript - Angular 翻译在 2 个 Controller 之间不起作用

javascript - document.getElementById ("someId") 比。一些ID

java - 页面未加载到 targetFrame 中

php - strip_shortcodes 不起作用

javascript - 在新选项卡中打开发布请求

javascript - 当按钮被禁用时 jQuery 抛出错误

javascript - 如何动态添加jquery选项卡