Http.get() working but not working in build(Releas

2020-03-26 08:49发布

I am trying get data from a simple api, it works fine in ionic serve(browser) , But when i build the app http call does not work. my Code is

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]);

          }
        }
   });

please help with this issue.

3条回答
对你真心纯属浪费
2楼-- · 2020-03-26 09:23

I think you can use Native HTTP plugin for the device use cases:

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

usage from the doc:

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);

  });
查看更多
ら.Afraid
3楼-- · 2020-03-26 09:35

I am Guessing that your are getting this because of android changes its http architecture.

to make it working on Android go to your project root folder.

yourAppFolder > resources > android > xml > network_security_config.xml Change your network security config to blow code.

<?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>
查看更多
啃猪蹄的小仙女
4楼-- · 2020-03-26 09:47

This works for me on the Ionic 4 app.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>
查看更多
登录 后发表回答