【发布时间】:2019-06-17 06:38:30
【问题描述】:
我正在用 Angular 编写一个 post 方法来发布一些我在 Codeigniter 中返回 JSON 的数据。
这是服务:
public userlogin(data) {
let uploadURL = `myurl`;
const requestOptions: Object = {
responseType: 'json'
}
return this.http.post<any>(uploadURL, data,requestOptions).pipe(map((responce) => {
console.log(responce);
return responce;
})
);
}
在我的组件中:
this.services.postuser(formData).subscribe(
(data) => {
console.log(data);
}
);
我经历了很多堆栈溢出问题和答案,但解决方案对我不起作用。 我还将 post 方法响应从 JSON 更改为 text 并将 post 方法中的响应类型更改为 text 仍然收到相同的错误。
后台功能:
public function user_login()
{
if(is_array($_POST) && sizeof($_POST) >0){
$where_array = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$result = $this->Admin_model->checkuser($where_array);
// print_r($result); die;
if($result == "user not found" || $result == "incorrect password")
{
$json_data = array("Code"=>"1","data"=>$result);
echo json_encode($json_data, JSON_UNESCAPED_SLASHES);
}else{
$json_data = array("Code"=>"0","data" => "Login successfull");
echo json_encode($json_data, JSON_UNESCAPED_SLASHES);
}
}
}
【问题讨论】:
-
错误响应也可以包含响应正文。这就是这里发生的事情。
-
能否详细说明。很多答案让我很困惑,我到底应该怎么做
-
official doc 拥有一切。向下滚动到错误处理。
-
根据官方文档我做了错误处理我得到了错误---->后端返回代码0,正文是:[object ProgressEvent]
标签: angular codeigniter http response