Ionic 4 Http 失败响应(未知 url):0 未知错误.在真机上调用api时 [英] Ionic 4 Http failure response for (unknown url): 0 Unknown Error. when calling api on real device

查看:23
本文介绍了Ionic 4 Http 失败响应(未知 url):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 证书!

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

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.

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

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