【问题标题】:Common mistake for unexpected token in json at position 0 json codeigniterjson codeigniter 位置 0 处 json 中意外令牌的常见错误
【发布时间】:2017-09-02 11:51:20
【问题描述】:

我正在使用 Codeigniter 使用 jquery/json 在我的项目中开发登录模块。我的用户表中有 4 个数据;即a,b,c,d,这些用户是指定状态(状态1,状态2,状态3,状态4)的管理员。

当用户 a 登录时,它会重定向到 localhost/xxxxx/administrator/state1/dashboard 等等。

我的代码运行较早,但现在我在位置 0 的 json 中遇到此错误意外令牌,我该如何解决此问题?

查看

 <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");
            } 
        }
    }

【问题讨论】:

    标签: php jquery json codeigniter


    【解决方案1】:

    如果要使用 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)
              }
            },
    

    【讨论】:

    • 我的不一样吗?我的 json_encode 被指示为 state1。它早些时候工作,但现在我不知道发生了什么:/
    • 那完全不一样,你对比过我的代码和你的代码然后试过了吗?
    • 您好,先生,是否可以有 2 个特定的密钥?例如,可以先 echo json_encode("success");另一个是 echo json_encode("state");
    猜你喜欢
    • 2020-04-23
    • 2020-11-20
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2021-07-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多