douyonglang4845 2016-01-03 12:35
浏览 25

如何使用Ajax并同时返回视图

I have a login method in my controller where I check if there is such user in database or not. I call this method when press Submit button. I show login from view at the same time.

In my case, it doesn't show message if there is such user. I think because in my controller I load view.

How could I show this message if there is such user using Ajax and if I return view as I do in my case? I'm using Kohana. Thanks!

My code is:

$(document).ready(function(){

  $('#submit').on('click', function() {

    if(username.length === 0 || password.length === 0) {
      //...check if validation fails
    }
    else {
      $.ajax({
        url: "/admin/signin" ,
        type: "POST",
        data: {
          "username":username,
          "password":password
        },
        success: function(data) {
          if(data !== 'error') {

            window.location = "/admin/index";
          }
          else
          {
            alert('no such user');
          }
        }
      });
    }

  });

});
public function action_signin()
{

if ($_POST) {
$is_admin = Model_Admin::signin($_POST);           

print 'success';
} else {
print 'error';
}
}

$this->template->content = View::factory('admin/login_form');


}

</div>
  • 写回答

2条回答 默认 最新

  • doushi1912 2016-01-04 13:54
    关注

    If you want not load 'default' template try using $this->auto_render = FALSE; also kohana controller has method is_ajax $this->request->is_ajax()

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题