Http failure response 0 Unknown error 错误分析
使用下面这段代码在 Angular 应用里进行 HTTP 访问:
this.httpClient.get(url).subscribe(response => {
//do something with response
}, err => {
console.log(err.message);
}, () => {
console.log('completed');
}
问题是,当请求失败时,我在控制台中看到 (unknown url): 0 Unknown Error
消息的通用 Http 失败响应。 同时,当我在 chrome 中检查失败的请求时,我可以看到响应状态为 422,并且在预览
选项卡中,我看到了描述失败原因的实际消息。
如何访问我可以在 chrome 开发工具中看到的实际响应消息?
这是演示问题的屏幕截图:
[图片]
一种可能的原因是请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此,不允许来自 Origin ‘http://localhost:4200’ 的应用访问该资源。
这意味着来自后端服务器的响应缺少 Access-Control-Allow-Origin 标头,即使后端 nginx 已配置为使用 add_header 指令将这些标头添加到响应中。
但是,此指令仅在响应代码为 20X 或 30X 时添加标头。 在错误响应中,标头丢失。 无论响应代码如何,我都需要使用 always 参数来确保添加标头:
add_header 'Access-Control-Allow-Origin' 'http://localhost:4200' always;
正确配置后端后,我可以访问 Angular 代码中的实际错误消息。
除了 CORS 错误之外,另一种可能的错误:
我的问题是因为我使用的是 Android 平台级别 28,默认情况下禁用明文网络通信,并且我正在尝试开发指向笔记本电脑 IP(运行 API 服务器)的应用程序。 API 基本 URL 类似于 http://[LAPTOP_IP]:8081
。 因为它不是 https,所以 android webview 完全阻止了手机/模拟器和我笔记本电脑上的服务器之间的网络传输。 为了解决这个问题,修改下列配置文件:
resources/android/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!-- Set application-wide security config -->
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>
<platform name="android">
...
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
....
</platform>
相关文章
- 如何监控微信小程序HTTP请求错误
- Http常见错误收集
- 检查错误的视频文件
- 新手学MVC----MVC3.0错误 HTTP 404
- 10g TNS 13541 监听错误 tnsping可以但是conn system/manager@mult1.net 报错
- 解决EasyDSS、EasyNVR流媒体RTMP、HLS(m3u8)、HTTP-FLV播放提示H5播放错误的问题
- Deepin v20依赖错误,检测更新失败解决办法
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- 如何使用事务码SMICM分析ABAP代码发起的HTTP请求的错误
- 使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
- 利用SMW01分析BDOC错误
- SAP Word template出了错误后的调试办法
- WCF HTTP 错误 404.3 - Not Found(由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。)
- System.ServiceModel.CommunicationException: 接收HTTP 响应时发生错误
- http status 400,http 400,400 错误
- SAP Fiori应用里出现http request错误的原因分析
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- http错误-413 Request Entity Too Large
- 通过GRE建立ospf邻居关系时,因为错误端口宣告导致的频繁重新建立
- Leetcode 645. 错误的集合
- 解决办法:错误异常The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path