Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a return value from http.post

Tags:

angularjs

My question is the following I have this code here:

$http.post("dologin", data).then(function(resposta)

When I give a console.log (response.data) it returns the following:

 string(40) "7f9870111b7c032f6203d144c97b8aa5a3e3f0c5"
 {"status":"success","type_user":"secretary"}

I need to get the "status" value. Before I was using the following and it worked fine

if (response.data.status === 'success') {
    if (answer.data.type_user === 'teacher') {
      $ location.path ('teacher_view');
    } else if (response.data.type_user === 'candidate') {
      $ location.path ('candidate_view');
    } else if (answer.data.type_user === 'secretary') {
      $ location.path ('secretaria_view');
    }

Now it does not work anymore and when I give console.log (response.data.status); it returns undefined

My backend php

    public function do_login(){
     $this->load->model('LoginM');
     $request_body = file_get_contents('php://input');
 $data = json_decode($request_body);
 $cpf = trim($data->cpf);
 $senha = trim($data->senha);
     $tipo = trim($data->tipo);

 $response = $this->LoginM->do_login($cpf, $senha, $tipo);

 if ($response["status"] === "success"){
        if ($tipo == 'admin') {
     $data = array(
         'logged_in' => true,
         'cod_user' => $response["cod_user"]
     );
     $message = array("status"=>"success", "tipo_user"=>"secretaria");
     }elseif ($tipo == 'professor') {
     $data = array(
         'logged_in' => true,
         'cod_professor' => $response["cod_professor"]
     );
     $message = array("status"=>"success", "tipo_user"=>"professor");
    }elseif ($tipo == 'candidato') {
     $data = array(
         'logged_in' => true,
         'cod_candidato' => $response["cod_candidato"]
     );
     $message = array("status"=>"success", "tipo_user"=>"candidato");
    }
   $this->session->set_userdata($data);
 }else{
   $message = array("status"=>"error","message"=>"CPF ou senha incorretos");
 }
 echo json_encode ($message);
}

Test with response from console.log (response.data) and console.log (response.data.status)

Image console.log

like image 825
Andre Lacomski Avatar asked Apr 25 '26 00:04

Andre Lacomski


1 Answers

Below thing should do the trick:

var state = JSON.parse('{'+res.data.split("{")[1]);

if (state.status === 'success') {
 if (state.type_user === 'teacher') {
  $ location.path ('teacher_view');
 } else if (state.type_user === 'candidate') {
  $ location.path ('candidate_view');
 } else if (state.type_user === 'secretary') {
  $ location.path ('secretaria_view');
}

But, i would strongly suggest you to fix your server response from string which you are getting right now, to the JSON which you were getting earlier

like image 160
Shashank Vivek Avatar answered Apr 27 '26 19:04

Shashank Vivek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!