angular - 加载资源失败:服务器响应状态为 404 (Not Found) ERROR HttpErrorResponse
问题描述
我目前正在开发一个 Angular 7 应用程序。当我尝试调用http post时,当web api返回成功消息时它工作正常,当api返回错误消息时我得到错误。消息返回类似于 {Error=true, Message="invalid data"}
服务.ts
addCustomer(customer: Customer) {
const body: Customer = {
// do...
};
return this.http.post(this.apiurl + 'Products/Post', body);
}
组件.ts
OnSubmit(form: NgForm) {
this.apiservice.addCustomer(form.value).subscribe((data: any) => {
if (data.Error === false) {
this.resetForm(form);
this.toastr.success(data.Message);
} else {
this.toastr.error(data.Message);`enter code here`
}
});
}
解决方案
我认为这是一个后端问题。如果您对后端大张旗鼓,请尝试在其上使用无效数据并查看响应。
更新
试试这个方法。我删除了,===false
因为您的第一条语句必须返回成功消息!else 语句处理错误!
OnSubmit(form: NgForm) {
this.apiservice.addCustomer(form.value).subscribe((data: any) => {
if (data.Error) {
this.resetForm(form);
this.toastr.success(data.Message);
} else {
this.toastr.error(data.Message);
}
});
}
推荐阅读
- c++ - 从类中打印变量
- java - 从动态更新的 Application.properties 中读取
- javascript - 使用 getText() 的量角器中的断言错误
- powerbi - 从两个切片器中查找最大月份
- r - 过滤R中的记录
- php - 在 Symfony 3.4 中上传文件 - 更新操作 - 我应该如何编程 editAction?
- angular - 变量未定义的可观察打字稿
- c# - 团队构建期间设置的 wixtool 获取 light.exe System.UnauthorizedAccessException:访问路径
- grafana - 如何将视频/流添加到 Grafana 仪表板?
- android - webview的iframe下拉刷新