Http failure response 0 Unknown error 错误分析

简介: 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>


相关文章
|
3月前
|
缓存 网络协议 数据库连接
C/S架构中HTTP错误状态码原因分析及解决办法
HTTP(Hypertext Transfer Protocol)是用于在客户端和服务器之间传输数据的协议。当在浏览器或其他HTTP客户端中访问网页时,可能会发生各种访问报错。我们需要根据网页提供的错误状态码分析错误原因,以找到相对应的解决办法。
41 0
|
3月前
|
移动开发 JSON 监控
网络协议解析:在员工上网监控软件中实现HTTP流量分析
随着企业对员工网络活动的监控需求不断增加,开发一套能够实现HTTP流量分析的网络协议解析系统变得愈发重要。本文将深入探讨如何在员工上网监控软件中实现HTTP流量分析,通过代码示例演示关键步骤。
221 0
|
2月前
|
机器学习/深度学习 前端开发 JavaScript
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
43 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
10月前
|
网络虚拟化 Docker 容器
本地docker不能登录远程harbor服务器,error response from daemon,error parsing http 403 response body
本地docker不能登录远程harbor服务器,error response from daemon,error parsing http 403 response body
136 0
本地docker不能登录远程harbor服务器,error response from daemon,error parsing http 403 response body
|
7月前
|
网络安全
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://xxxx.svc.cluster.local:8080/xxxx": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "xxxx.svc.cluster.local:8080/xxxx ": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
910 0
|
4月前
|
开发框架 .NET 数据库连接
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
解决HTTP错误500.19 - internal server error -内部服务器错误的终极指南
700 0
|
23天前
|
内存技术
HTTP-FLV详解及分析(二)
HTTP-FLV详解及分析
23 0
|
6月前
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
4月前
|
网络协议
Wireshark中的http协议包分析
Wireshark可以跟踪网络协议的通讯过程,本节通过http协议,在了解Wireshark使用的基础上,重温http协议的通讯过程。 TCP(Transmission Control Protocol,传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。 HTTP(HyperText Transfer Protocol,超文本传输协议)是一种用于分布式、协作式和超媒体信息系统的应用层协议,是万维网的数据通信的基础。 下图是访问百度页面的头部文件的Wireshark数据包截取图,以下几点说明如下:
Wireshark中的http协议包分析
|
6月前
|
应用服务中间件 开发工具 nginx
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
165 0