ajax请求正在运行并访问成功函数,问题是当我尝试使用echo json_encode($data)提供的数据时。

$data['status']$data['error_list']

我遇到以下错误:未捕获的ReferenceError:数据未定义

你们能告诉我为什么会这样吗?

控制器:

    public function get_user()
    {
        $result = false;
        $data["error_list"] = array();
        $data["status"] = false;

        $email = $this->input->post("email");
        $password = $this->input->post("password");


        if (empty($email)) {
            $data["error_list"] = "O email deve ser preenchido";
        } else {
            $this->load->model("M_login");
            $result = $this->M_login->get_user($email);
        }


        if($result)
        {
            if(password_verify($password, $result->password))
            {
                $this->session->set_userdata("user_id", $result->id);
                $this->session->set_userdata("user_name", $result->nome);
                $data["status"] = true;
            } else {
                $data["error_list"]= "Credenciais invalidas";
            }
        }
        else
        {
            $data["error_list"] = "Credenciais invalias";
        }
        echo json_encode($data);

    }



Ajax请求:

    $(function(){

          // quando ocorrer o submite no form esse evento sera carregado
          $("#login-formulario").submit(function(){

              //chamando a funcao ajax
              $.ajax({
                  type: "post", //tipo da requisicao
                  url: BASE_URL + "login/get_user", //url que será chamada
                  dataType: "JSON",
                  data: $(this).serialize(),
                  beforeSend: function(){
                      clearError();
                      $("#loading").html(loadingImg());
                  },
                  success: function(){
                      if(data['status'] == true){
                          clearError();
                          $("#loading").html(loadingtrue());
                       }
                      else{

                          ShowError(data['error_list']);
                      }
                  },
                  error: function(response){
                     console.log(response);
                  }
              })
              return false;
          })
      })

最佳答案

我希望这对您有帮助

                 success: function(data){
                      if(data.status == true){
                          clearError();
                          $("#loading").html(loadingtrue());
                       }
                      else{

                          ShowError(data.error_list);
                      }
                  },

关于php - CodeIgniter不会从 Controller 到Ajax获取回波数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60328644/

10-12 14:03
查看更多