【问题标题】:CodeIgniter Ajax Form PostCodeIgniter Ajax 表单发布
【发布时间】:2014-02-11 15:34:34
【问题描述】:

我正在使用 CodeIgniter 和 jQuery Forrm 插件:http://malsup.com/jquery/form/

我在让表单正常工作时遇到问题。

查看

<div class="row">
    <div class="well col-sm-5 col-sm-offset-3">
        <h2><span class="fa fa-key"></span>&nbsp;&nbsp;Please login below</h2>
        <form class="form-horizontal" id="LoginForm" role="form" method="post" action="/login_controller/process" onsubmit="PostFormRedirect('#LoginForm', '#RetMsg', '/');return false;">
            <div class="row small-pad">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-inbox" style="max-width:20px;min-width:20px;"></i></span>
                    <input type="email" class="form-control" id="UserName" name="UserName" placeholder="Email" />
                </div>
            </div>
            <div class="row small-pad">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-key" style="max-width:20px;min-width:20px;"></i></span>
                    <input type="password" class="form-control" id="UserPass" name="UserPass" placeholder="Password" />
                </div>
            </div>
            <div class="row small-pad">
                <button type="submit" class="btn btn-primary pull-right"><span class="fa fa-sign-in"></span>&nbsp;&nbsp;Sign in</button>
            </div>
            <div class="row small-pad center-text">
                <a href="/forgot" class="is-ajax-inner" data-where="#RetMsg">Forgot My Password</a>
            </div>
        </form>
        <div id="RetMsg"></div>
    </div>
</div>

控制器

class Login_controller extends CI_Controller {
    
    public function __construct(){
        $this->load->database();
        var_dump('loaded');
        exit;
    }
    
    public function process(){
        $this->load->model('login_model');
        $this->login_model->process();
    }
    
}

型号

class Login_model extends CI_Model {
    
    function process(){
        var_dump('YEP');
        exit;
    }
    
}

PostFormRedirect 函数

function PostFormRedirect(FormID, Target, Location){
    try{
        $subButton = jQuery('[type="submit"]');
        var options = {
            target: Target,
            beforeSubmit: function () {
                $subButton.attr('disabled', 'disabled');
                jQuery(Target).html('<div class="pageLoader">Loading...<br /><img src="/assets/images/ajax-loader.gif" alt="loading..." height="11" width="16" /></div>');
                jQuery(Target).slideDown('fast');
            },
            success: function (html) {
                setTimeout(function () {
                                        var $t = Math.round(new Date().getTime() / 1000);
                                        jQuery(Target).html(html);
                                        jQuery(Target).slideUp('fast'); 
                                        if(typeof(Location) !== 'undefined'){
                                            window.location.href = Location + '?_=' + $t;
                                        }
                                     }, 3000);
            },
            error: function(e){
                var $html = e.responseText;
                jQuery(Target).html($html);
                jQuery(Target).slideDown('fast');
                if(jQuery('#captcha-gen').length){
                    var $t = Math.round(new Date().getTime() / 1000);
                    jQuery.get('/inc/captcha.php?_=' + $t, function(data){
                        jQuery('#captcha-gen').html(data);
                    });
                }
                $subButton.removeAttr('disabled');
            }
        };
        jQuery(FormID).ajaxSubmit(options);
    }catch(err){
        alert(err.message);
    }
}

.htaccess

# The Friendly URLs part - for normal pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA,NC]

config/routes.php

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';

现在发生的事情是,RetMsg 向我显示 404 错误...当我可以摆脱 404 错误时,RetMsg 再次向我显示登录表单。

我做错了什么?我希望它会告诉我var_dump('YEP');

【问题讨论】:

  • 问题可能出在你的routes.php

标签: javascript php jquery ajax codeigniter


【解决方案1】:

如果与routes.php 没有问题,请尝试通过 URL(手动)调用您的控制器。


请仔细看这部分代码...action="/login_controller/process" onsubmit="PostFormRedirect('#LoginForm', '#RetMsg', '/');...

尝试编辑action="&lt;?= base_url('login_controller/process')?&gt; 的操作,base_url()url 帮助器中(自动加载)。


一些调试技巧:

在控制器分析器$this-&gt;output-&gt;enable_profiler(TRUE); 中启用不幸的是,您的控制器必须正确加载(没有 404 错误),但这可能会在将来帮助您,即使在 AJAX 调用中(您可以看到发送了哪些 POST 数据)。在实际应用中禁用此功能,因为 JSON 数据将被“损坏”。

对于 AJAX 测试,请使用 Google Chrome(或 FF),右键单击并检查元素查看网络选项卡(您已经知道这一点)但是现在,每当您进行 AJAX 调用时,都会出现带有标题/正文的行等在标题中,您可以看到调用了什么 URL,因此“为什么”出现 404 错误。

在构造函数中使用 if/else 语句忽略非 AJAX 调用

    public function __construct() {
        parent::__construct();
        if (!$this->input->is_ajax_request()) {

            redirect(); //no ajax redirect to home

        }

        $this->output->enable_profiler(FALSE); //force to disable profiler

    }

【讨论】:

  • 我必须添加一条路线。我从未见过is_ajax_request... 那会非常方便:) 谢谢!
猜你喜欢
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2020-10-26
  • 2016-12-14
相关资源
最近更新 更多