Ionic 未收到 API 的响应
Ionic NOT getting response from API
我实际上是在使用 IONIC 框架构建应用程序。我正在尝试从 API 中取回一些值,该值正在离子(离子服务)的网络视图上找到,但是当我 运行 命令 离子电容器 运行 android 它不适用于 android 版本并且返回以下错误:
{"headers": {"normalizedNames":{},"lazyUpdate": null, "headers":{}},"status": 0, "statusText": "Unknown Error", "url": "https://xxx.xxx.xxx.xxx/company","ok":"false","name":"HttpErrorResponse","message" : "Http failure response for http://xxx.xxx.xxx.xxx/company: 0 Unknown Error","error": {"isTrusted":true}}listing
这是 http.service.ts 文件中的代码:
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
@Injectable({
providedIn: "root",
})
export class HttpService {
constructor(private http: HttpClient) {}
private URL = "http://xxx.xxx.xxx.xxx/company";
getCompany() {
return this.http.get(this.URL).toPromise();
}
}
这是来自 home.page.ts 的代码:
import { HttpService } from "../services/http.service";
this.http
.getCompany()
.then((response) => {
console.log(response);
this.companies_array = JSON.parse(JSON.stringify(response));
this.is_loading = false;
})
.catch((response) => {
this.err_msg = JSON.stringify(response);
});
我在 app.module.ts 中添加了导入:
import { HttpClientModule } from "@angular/common/http";
我添加了一个文件调用 proxy.conf.json,我在其中添加了以下代码:
{
"/api/*": {
"target": "http://xxx.xxx.xxx.xxx",
"secure": false,
"logLevel": "debug"
}
}
我修改了 angular.json 文件,其中:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "app:build",
"proxyConfig": "proxy.conf.json"
},
"configurations": {
"production": {
"browserTarget": "app:build:production"
},
"ci": {
"progress": false
}
}
},
我遇到了同样的问题,你能解决类似的问题吗?您只需添加一行,即
android:usesCleartextTraffic="true"
在您的清单文件中,即
[你的项目]/android/app/src/main/AndroidManifest.xml
我实际上是在使用 IONIC 框架构建应用程序。我正在尝试从 API 中取回一些值,该值正在离子(离子服务)的网络视图上找到,但是当我 运行 命令 离子电容器 运行 android 它不适用于 android 版本并且返回以下错误:
{"headers": {"normalizedNames":{},"lazyUpdate": null, "headers":{}},"status": 0, "statusText": "Unknown Error", "url": "https://xxx.xxx.xxx.xxx/company","ok":"false","name":"HttpErrorResponse","message" : "Http failure response for http://xxx.xxx.xxx.xxx/company: 0 Unknown Error","error": {"isTrusted":true}}listing
这是 http.service.ts 文件中的代码:
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
@Injectable({
providedIn: "root",
})
export class HttpService {
constructor(private http: HttpClient) {}
private URL = "http://xxx.xxx.xxx.xxx/company";
getCompany() {
return this.http.get(this.URL).toPromise();
}
}
这是来自 home.page.ts 的代码:
import { HttpService } from "../services/http.service";
this.http
.getCompany()
.then((response) => {
console.log(response);
this.companies_array = JSON.parse(JSON.stringify(response));
this.is_loading = false;
})
.catch((response) => {
this.err_msg = JSON.stringify(response);
});
我在 app.module.ts 中添加了导入:
import { HttpClientModule } from "@angular/common/http";
我添加了一个文件调用 proxy.conf.json,我在其中添加了以下代码:
{
"/api/*": {
"target": "http://xxx.xxx.xxx.xxx",
"secure": false,
"logLevel": "debug"
}
}
我修改了 angular.json 文件,其中:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "app:build",
"proxyConfig": "proxy.conf.json"
},
"configurations": {
"production": {
"browserTarget": "app:build:production"
},
"ci": {
"progress": false
}
}
},
我遇到了同样的问题,你能解决类似的问题吗?您只需添加一行,即
android:usesCleartextTraffic="true"
在您的清单文件中,即
[你的项目]/android/app/src/main/AndroidManifest.xml