这是我从WebAPI返回数据的方式:
[HttpGet]
[Route("ProcessHistory")]
public HttpResponseMessage ProcessHistory()
{
HttpResponseMessage response = new HttpResponseMessage();
try
{
IProcessManagement ProcessManagement = serviceFactory.CreateProcessManagement();
List<Process> listProcess = ProcessManagement.Get().ToList();
response = Request.CreateResponse<IEnumerable<Process>>(HttpStatusCode.OK, listProcess);
}
catch (Exception ex)
{
response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
return response;
}
角度:
调用Angular服务的Angular Component方法:
getProcessScheduleStatus(){
this.Service.getProcessScheduleStatus(this.ApiUrl + ConstAPIUrls.ProcessScheduleStatus).subscribe(
(next: ProcessScheduleStatus[]) => {
alert('Got status successfully..!!');
console.log('Processes: ');
console.log(next);
this.result.IsSuccess = true;
this.result.SuccessMessage = 'Success';
this.openModal(this.result);
},
error => {
alert('Error');
if (error.status == 400) {
this.result.IsSuccess = false;
this.result.ErrorMessage = JSON.parse(JSON.stringify(error.error));
this.openModal(this.result);
}
else {
//Parse response for other error status
this.result.IsSuccess = false;
this.result.ErrorMessage = JSON.parse(JSON.stringify(error.message));
this.openModal(this.result);
}
},
() => {
//process to be run after web api request completes
}
);
}
正在调用Web api的角度服务:
getProcessScheduleStatus(apiUrl: string):Observable<ProcessScheduleStatus[]>{
return this._httpClient.get(apiUrl, this.httpOptions).map((result: ProcessScheduleStatus[]) => {
alert('web api execued');
if (result != null && result != undefined) {
return result;
}
else {
return result;
}
});
}
当Angular服务访问Web api时,但当Web api返回响应时,它将直接转到组件错误处理程序。
如何从响应中正确读取数据?
Chrome XHR中的故障请求标头: