gpt4 book ai didi

angularjs - 从 http.post 获取返回值

转载 作者:行者123 更新时间:2023-12-02 02:44:59 26 4
gpt4 key购买 nike

我的问题如下我这里有这段代码:

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

当我给出 console.log (response.data) 时,它返回以下内容:

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

我需要获取“状态”值。在我使用以下内容之前它运行良好

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

现在它不再工作了,当我给 console.log (response.data.status);它返回未定义

我的后端 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);
}

使用来自 console.log (response.data) 和 console.log (response.data.status) 的响应进行测试

Image console.log

最佳答案

下面的东西应该可以解决问题:

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

但是,我强烈建议您将服务器响应从您现在获得的字符串修复为您之前获得的 JSON

关于angularjs - 从 http.post 获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com