我正在使用jquery/json使用Codeigniter在我的项目中开发一个登录模块。我的users表中有4个数据,即a、b、c、d,这些用户是指定状态的管理员(状态1、状态2、状态3、状态4)。
当用户登录时,它将重定向到localhost/xxxxx/administrator/state1/dashboard等。
我的代码运行得更早,但是现在我在json 0位置遇到了这个错误意外令牌,我如何解决这个问题?
视图
<div id="dialog" class="dialog dialog-effect-in">
<div class="dialog-front">
<div class="dialog-content">
<form id="login-form" class="dialog-form" method="POST">
<fieldset>
<legend>Log in</legend>
<div class="form-group">
<label for="user_username" class="control-label">Username:</label>
<input type="text" id="email" class="form-control" name="email" autofocus/>
</div>
<div class="form-group">
<label for="user_password" class="control-label">Password:</label>
<input type="password" id="password" class="form-control" name="password"/>
</div>
<div class="pad-top-20 pad-btm-20">
<input type="submit" class="btn btn-default btn-block btn-lg" value="Continue">
</div>
</fieldset>
</form>
</div>
</div>JS
function login_error_message(idname,message) {
$(idname).html('<div class="alert alert-danger text-center" role="alert">' + message +
'</div>');
}
$(document).ready(function(){
$("#login-form").on('submit',function(e){
$.ajax({
url: base_url + 'administrator/login',
data: $(this).serialize(),
type: "POST",
success: function(data)
{
var result = JSON.parse(data);
if(result === "state1")
{
$("#validate_error").html("");
window.location.href=base_url+"administrator/state1/dashboard";
}
else{
login_error_message(".alert-login",result)
}
},
error: function(data)
{
alert('Opps! Something went wrong. please contact the administrator. ');
},
})
e.preventDefault();
});
});控制器
public function login() {
if($this->form_validation->run('login_validate') == FALSE) {
echo json_encode(validation_errors());
} else {
$email = clean_data($this->input->post('email'));
$password = clean_data($this->input->post('password'));
$where = array('email'=>$email);
$get_user = $this->Crud_model->fetch_tag_row('*','users',$where);
if($get_user) {
$check_password = $get_user->password;
$get_state = $get_user->state;
if(password_verify($password,$check_password)) {
if($get_user->status == 'Active') {
if($get_state=="state1"){
$user_session = [
'id' => $get_user->id,
'email' => $get_user->email,
'first_name' => $get_user->first_name,
'middle_name' => $get_user->middle_name,
'last_name' => $get_user->last_name,
'state' => $get_user->state
];
$this->session->set_userdata('logged_in',$user_session);
$session = $this->session->userdata('logged_in');
$this->session->id = $session['id'];
$this->session->email = $session['email'];
$this->session->fullname = $session['first_name'] .' '. $session['middle_name'] .' '. $session['first_name'];
$this->session-state = $session['state'];
echo json_encode("state1");
}
}elseif($get_user->status == 'Inactive'){
echo json_encode("Your account is inactive. Contact our human resource department regarding this problem.");
}
}else {
echo json_encode("Invalid Credentials");
}
}else{
echo json_encode("Invalid Credentials");
}
}
}发布于 2017-09-02 12:00:31
如果要使用JSON作为响应,则需要指定键,例如:
控制器
$r = [
'status' => 'invalid'
];
echo json_encode($r);JS
success: function(data)
{
var result = JSON.parse(data);
if(result.status === "invalid")
{
$("#validate_error").html("");
window.location.href=base_url+"administrator/state1/dashboard";
}
else{
login_error_message(".alert-login",result)
}
},https://stackoverflow.com/questions/46013470
复制相似问题