skip to Main Content

The ajax request is working and access the success function, the problem is when I try to use the data provide by the echo json_encode($data).

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

I having the follow error : Uncaught ReferenceError: data is not defined

Can you guys tell me why it’s happening ?

Controller:

    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 request:

    $(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;
          })
      }) 

3

Answers


  1. Correct your declaration

    $data = array();
    $data["error_list"] = array();
    $data["status"] = false;
    
    Login or Signup to reply.
  2. In Ajax Success Function you didn’t get the response from controller

       success: function(ResponseData){
               if(ResponseData.status == true){
                          clearError();
                          $("#loading").html(loadingtrue());
                       }
                      else{
                          ShowError(ResponseData.error_list); 
                      }      
          }
    
    Login or Signup to reply.
  3. i Hope this help you

                     success: function(data){
                          if(data.status == true){
                              clearError();
                              $("#loading").html(loadingtrue());
                           }
                          else{
    
                              ShowError(data.error_list); 
                          }
                      },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search