(未知url)的离子4 Http故障响应:0未知错误.在真实设备上调用api时 [英] Ionic 4 Http failure response for (unknown url): 0 Unknown Error. when calling api on real device

查看:475
本文介绍了(未知url)的离子4 Http故障响应:0未知错误.在真实设备上调用api时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ionic 4应用程序,它调用.NET Core后端api. 它在chrome浏览器上正常工作,但是当我在android设备上运行apk时,响应为:

I have an ionic 4 application which calls a .NET Core backend api. It works correctly on chrome browser, but when I run apk on android device, the response is:

{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}

关于标题,我附加了以下选项:

regarding to header I appended these options:

const httpOptions = {
    headers: new HttpHeaders({
        "Content-Type": "application/x-www-form-urlencoded; charset=utf-8" , 
        "Access-Control-Allow-Origin": "*", 
        "Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token, Accept, Authorization, X-Request-With",
        "Access-Control-Allow-Credentials" : "true",
        "Access-Control-Allow-Methods" : "GET, POST, DELETE, PUT, OPTIONS, TRACE, PATCH, CONNECT"  
       }) 
};  

我已经安装了插件: cordova-plugin-ionic-webview

I have already installed plugin: cordova-plugin-ionic-webview

并使用来自"@ angular/common/http"的HttpClient

and using HttpClient from "@angular/common/http"

我的API远程托管,未使用ssl证书!

My API hosted remotely, no ssl certificate used !

我搜索了所有解决方案,但没有一个解决我的问题

I googled all solutions, but none of them solve my problem

推荐答案

最后,经过两天的努力,我可以解决问题! 我将API调用从'@ angular/common/http'迁移到了本地http'@ ionic-native/http/ngx' 带有此标头:

Finally, I could resolve problem after two days hardwork ! I migrated API calling from '@angular/common/http' to native http '@ionic-native/http/ngx' with this header:

        // create headers data
        const headers = {
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", 
          'Accept': 'application/json, text/plain',
          "cache-control": "no-cache", 
          "Access-Control-Allow-Origin": "*", 
          "Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token, Accept, Authorization, X-Request-With, Access-Control-Request-Method, Access-Control-Request-Headers",
          "Access-Control-Allow-Credentials" : "true",
          "Access-Control-Allow-Methods" : "GET, POST, DELETE, PUT, OPTIONS, TRACE, PATCH, CONNECT",  
          };

对我来说不利,目前,我必须在真实设备上进行测试.

The cons for me, For now on I have to test on a real device.

这篇关于(未知url)的离子4 Http故障响应:0未知错误.在真实设备上调用api时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆