用户名和密码通过ajax登录..无法登录
问题描述:
控制器代码如下所示无法登录。 没有任何错误得到什么问题,我可以不知道如何通过Ajax用户名和密码通过ajax登录..无法登录
public function loginn()
{
$email=$this->input->post('email');
$password=$this->input->post('password');
//$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->login($email, $password)) {
$this->welcome();
}
else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
$this->index();
}
}
下面的代码中调用如图浏览页面通过ajax.script作为below.how帖子发送值传递后值控制器使用ajax。
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
//e.preventDefault();
var email = $("#email").val();
var password= $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
data: {email:email,password:password},
success:function(data)
{
alert('Successfully login');
},
error:function()
{
alert('fail');
}
});
});
});
在图像上点击登入画面下方 enter image description here
显示在图所示 enter image description here
答
看来你正在使用笨框架。因此,我对您的Ajax URL路径有疑问。
你给
url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
但笨应该像
url: "<?php echo base_url() ?>"+"/Login_cntrl/loginn",
OR
url: "<?php echo base_url('Login_cntrl/loginn') ?>",
它也可能是你调用从脚本文件这阿贾克斯。这不是一个PHP文件。所以base_url()函数在那里不起作用。在这种情况下,您必须将base_url以隐藏格式保存到输入变量中。然后应该在你的ajax代码中获取。
<input type="hidden" name="myurl" value="<?php echo base_url();">
,然后阿贾克斯一个
var myurl = $("#myurl").val();
+0
对不起,先生你的代码无法正常工作 –
答
你必须分开你的基本的登录操作和Ajax登陆行动,因为同时发送不同的响应。
PHP控制器:
public function ajax_login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->login($email, $password)) {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is successful
->set_output(json_decode(array('status' => 'success', 'message' => 'PUT_YOUR_SUCCESS_MESSAGE_HERE')))
;
} else {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is failed
->set_output(json_decode(array('status' => 'error', 'message' => 'PUT_YOUR_ERROR_MESSAGE_HERE')))
;
}
}
的Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url('Login_cntrl/ajax_login') ?>",
data: {email:email, password:password},
success: function(data) {
data = JSON.parse(data);
// success in ajax does not mean successfully login, so check again
if (data.status == 'success') {
alert(data.message]); // login success message defined in action
} else {
alert(data.message); // login failed message defined in action
}
},
error: function() {
// error in ajax means HTTP error, not login error
alert('Request failed!');
}
});
});
});
</script>
把你的jQuery代码 – Shayan
问题是什么?错误?问题是什么?预期与实际行为?由于缺乏细节而投票结束。 –
如果您有任何问题,请检查您的AJAX响应和PHP错误。也许这会帮助你找出问题所在。 – mi6crazyheart