【问题标题】:Http request working on ionic serve but doesn't work on "ionic cordova run android --device"在 ionic serve 上工作的 Http 请求,但在“ionic cordova run android --device”上不起作用
【发布时间】:2019-10-21 01:32:57
【问题描述】:

我正在使用 Ionic 本机 http 来发出服务器请求。服务器托管在 aws 上,并且 API 已启动并正在运行(由邮递员验证)。

当我执行 ionic 服务时,请求会通过并工作,但是当我尝试在设备上运行它时它不起作用。经检查,我得到“net::ERR_CLEARTEXT_NOT_PERMITTED”,帖子作为选项发送

ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://18.220.143.133/api/login", ok: false, ...}

已经尝试添加到 config.xml

login(user: User): Observable<AuthResponse> {
return this.httpClient.post('http://18.220.143.133/api/login', user, {
  headers: new HttpHeaders().set('Content-Type', 'application/json'),
}).pipe(
  tap(async (res: AuthResponse) => {

    if (res.success == true) {

      await this.storage.set('user',res.user);

       await this.storage.set('ACCESS_TOKEN', res.token);
      this.authSubject.next(true);
    }

  })
);

}

我希望移动设备也能通过 http 请求。

【问题讨论】:

标签: rest http ionic-framework ionic4


【解决方案1】:

同样的事情发生在我身上,直到我在 config.xml 文件中添加:

<preference name = "android-targetSdkVersion" value = "27" />

【讨论】:

    【解决方案2】:

    这与 API 调用的 CORS 有关。 This 可能会有所帮助。此外,由于您使用的是 Angular HTTP 请求,请在此处查看HTTP request from angular will send with method OPTIONS instead of POST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多