【问题标题】:CodeIgniter ajax call using jquery when call redirect it prints the whole page i divCodeIgniter ajax 调用在调用重定向时使用 jquery 打印整个页面 i div
【发布时间】:2012-02-27 03:03:53
【问题描述】:

没有适合这个错误的标题,但它是这样的,当按下 jquery ajax 调用控制器时,我有一个带有提交按钮的表单,如果它失败,则表单验证完成如果它通过,则重新绘制表单页面被重定向到带有 Flash 消息成功的主页,这就是错误发生的地方,它会在内容中重绘整个页面(页眉页眉页脚页脚)。我希望眼见为实是有道理的,所以这里是代码

旁注:“autform”是用于创建表单的库,“rest”是用于模板的库。

jquery 代码:

$("form.user_form").live("submit",function() {
  $("#loader").removeClass('hidden');
  $.ajax({
      async :false,
      type: $(this).attr('method'),
      url: $(this).attr('action'),
      cache: false,
      data: $(this).serialize(),
      success: function(data) {
                $("#center").html(data);
                     $('div#notification').hide().slideDown('slow').delay(20000).slideUp('slow');
                }
  })

  return false;
 }); 

控制器

 function forgot_password()
{
        $this->form_validation->set_rules('login',lang('email_or_login'), 'trim|required|xss_clean');
        $this->autoform->add(array('name'=>'login', 'type'=>'text', 'label'=> lang('email_or_login')));
        $data['errors'] = array();
        if ($this->form_validation->run($this)) {                               // validation ok
            if (!is_null($data = $this->auth->forgot_password(
                    $this->form_validation->set_value('login')))) {
                    $this-> _show_message(lang('auth_message_new_password_sent'));
            } else {
                $data['message']=$this-> _message(lang('error_found'), false);  // fail
                $errors = $this->auth->get_error_message();
                foreach ($errors as $k => $v){
                     $this->autoform->set_error($k, lang($v));
                }
            }
        }
        $this->autoform->add(array('name'=>'forgot_button', 'type'=>'submit','value' =>lang('new_password')));
        $data['form']=  $this->autoform->generate('','class="user_form"');
        $this->set_page('forms/default', $data);
        if ( !$this->input->is_ajax_request()) { $this->rest->setPage(''); }
        else { echo  $this->rest->setPage_ajax('content');       }
    }
}

function _show_message($message, $state = true)
{
    if($state)
    {
        $data = '<div id="notification" class="success"><strong>'.$message.'</strong></div>';
    }else{
        $data = '<div id="notification" class="bug"><strong>'.$message.'</strong></div>';
    }
    $this->session->set_flashdata('note', $data);
    redirect(base_url(),'refresh');
}

我认为好像重定向调用被 ajax 捕获,而不是向我发送主页,而是在表单的位置加载主页。

感谢您的帮助 问候

【问题讨论】:

    标签: php jquery ajax codeigniter redirect


    【解决方案1】:

    好的,找到了问题和解决方案,似乎你不能在试图将一大块 HTML 返回到 div 的 Ajax 调用中间调用重定向,结果会将重定向的 HTML 放置在 div 中。

    PhilTemhttp://codeigniter.com/forums/viewthread/210403/ 建议的解决方案 是当您想要重定向并且调用由 Ajax 进行然后将带有重定向 URI 的值返回给 Ajax 并让它重定向时。

    对于任何对代码感兴趣的人:

    Jquery Ajax 代码:

    $("form.user_form").live("submit", function(event) {
      event.preventDefault();
      $("#loader").removeClass('hidden');
      $.ajax({
          type: $(this).attr('method'),
          url: $(this).attr('action'),
          cache: false,
          dataType:"html",
          data: $(this).serialize(),
          success: function(data) {
              var res = $(data).filter('span.redirect');
              if ($(res).html() != null) {
                  [removed].href=$(res).html();
                  return false;
              }
              $("#center").html(data);
          },
          error: function() {
    
          }
      })
      return false;
    });  
    

    PHP 控制器

    function _show_message($message, $state = true, $redirect = '')
    {
        if ($state)
        {
            $data = '<div id="notification" class="success"><strong>'.$message.'</strong></div>';
        } else {
            $data = '<div id="notification" class="bug"><strong>'.$message.'</strong></div>';
        }
    
        $this->session->set_flashdata('note', $data);
    
        if ( !$this->input->is_ajax_request())
        {
            redirect(base_url() . $redirect, 'location', 302);
        }
        else
        {
             echo '<span class="redirect">'.base_url().$redirect.'</span>';
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      只使用个别错误

      json_encode(array(
         'fieldname'  =>  form_error('fieldname')
      ));
      

      AJAX

      success: function(cb)
      {
          if(fieldname)
          {
             {fieldname}.after(cb.fieldname)
          }
      }
      

      this

      【讨论】:

      • 我将大块的 html 发送回表单,这有助于保持代码简短,并且我使用一个 jquery 函数来处理我的所有表单。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      相关资源
      最近更新 更多