【问题标题】:Http.get() working but not working in build(Release/Debug) in Ionic 4Http.get() 在 Ionic 4 的构建(发布/调试)中工作但不工作
【发布时间】:2020-04-05 23:11:12
【问题描述】:

我正在尝试从一个简单的 api 获取数据,它在 ionic serve(browser) 中运行良好,但是当我构建应用程序时 http 调用不起作用。我的代码是

this.http.get("http://example.com/api/routes").subscribe(response => {
 this.routes = response["routes"];
 for (let x in this.routes) {
 let a = this.routes[x].rou_stops;
 let b = a.split(",");
          for (let y in b) {
            this.newCit.push(b[y]);

          }
        }
   });

请帮助解决这个问题。

【问题讨论】:

  • 任何运行时错误?
  • 不,先生,没有运行时错误。
  • 您的设备调试控制台是否出现任何错误?
  • 没有先生@NajamUsSaqib,调试中没有任何错误

标签: angular typescript http ionic-framework ionic4


【解决方案1】:

这些答案中的大多数虽然可以工作,但不是安全的选择,因为它们会打开非 https 流量。

明文仅用于实时重新加载的本地测试,不应用于生产应用。

真正的解决方案是使用 HTTPS 端点或通过 let's encrypt 在您自己的服务器上设置 HTTPS。

【讨论】:

    【解决方案2】:

    IONIC 5 更新

    我在 Ionic 5 中遇到了同样的问题,但由于 HttpClient 没有捕捉到错误(我只是返回一个未知的失败响应),这一事实变得更加复杂。

    为了查看它确实是一个 cleartext 错误,我首先必须为 Native HTTP 插件更改 HttpClient。

    在 Ionic 5 中,您现在可以在电容器.config.json 文件中启用明文或将现有的 json 文件替换为 .ts 版本:

    import { CapacitorConfig } from '@capacitor/cli';
    
    const config: CapacitorConfig = {
                     appId: 'com.company.appname',
                     appName: 'My Capacitor App',
                     webDir: 'www',
                     server: {
                               'cleartext': true
                             }
                  };
    
    export default config;
    

    docs

    【讨论】:

    • 它的工作,你节省了我的日子。我使用了相同的 Angular HttpClient。
    • 这也解决了 SocketIO 和 Ionic 问题。
    【解决方案3】:

    这适用于 Ionic 4 应用程序。

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">localhost</domain>
        </domain-config>
    </network-security-config>
    

    【讨论】:

    • 我会试试这个。
    【解决方案4】:

    我猜你得到这个是因为 android 改变了它的 http 架构。

    要使其在 Android 上运行,请转到您的项目根文件夹。

    yourAppFolder > 资源 > android > xml > network_security_config.xml 更改您的网络安全配置以破坏代码。

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true">
            <trust-anchors>
                <certificates src="system" />
            </trust-anchors>
        </base-config>
    </network-security-config>
    

    【讨论】:

    • 很高兴听到这个消息。 :)
    • 我没有 xml 文件夹
    • 你有什么目录结构?您使用的是哪个版本的 IONIC?
    【解决方案5】:

    我认为您可以将Native HTTP plugin 用于设备用例:

    ionic cordova plugin add cordova-plugin-advanced-http
    npm install @ionic-native/http
    

    文档中的用法:

    import { HTTP } from '@ionic-native/http/ngx';
    
    constructor(private http: HTTP) {}
    
    ...
    
    this.http.get('http://ionic.io', {}, {})
      .then(data => {
    
        console.log(data.status);
        console.log(data.data); // data received by server
        console.log(data.headers);
    
      })
      .catch(error => {
    
        console.log(error.status);
        console.log(error.error); // error message as string
        console.log(error.headers);
    
      });
    

    【讨论】:

    • 我使用 import { HttpClient } from "@angular/common/http";这是个问题吗?它以前没有问题。 :(
    • 有时基于浏览器的HttpClient 在本机设备上不起作用。这时候就需要使用原生的Http插件了。
    • 它有效,但是需要一些额外的更新更新。谢谢。
    • 是的,我认为配置更改更好,这是一个新事物,我今天学习了。
    • 与 OP 无关。过去我不得不使用这个插件,但 IONIC5 现在允许由 Angular HttpClient 和拦截器处理 HTTP 请求(感谢上帝)。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2015-12-28
    • 2015-09-12
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多