Started a new ionic app. I fetch json data from a json file hosted on the web. Like http://www.mydomain.ch/abc/data.json . Works well in browser as proxy is defined to redirect localhost to correct url. But in emulator or on device it fails, as it uses the url with localhost, and therefore of course does not find anything. Been searching around for days now… help appreciated.
Have tried so far without success: removing cordova plugins webview, inappbrowser; using older angular/http instead of angular/common/http; remove proxy entry and set whole path in provider (then it fails in browser, but does still not work on device); emulator iOS as well as Android fails, device so far only on Android tested.
Thanks a lot for hints or working examples of similar approaches. Best regards, Michael
Hi Peker,
as far as I know with --livereload the app is running on the device like on your desktop after “ionic serve” that is why it works as the proxy setting is getting used instead of looking for localhost.
What do you mean with “works fine” with https://ionicframework.com/docs/native/http/ - then your problem would be solved, isn’t it?
Cheers Michael
Hi JobMichael, https://ionic.zone/debug/remote-debug-your-app#android
when I look at the logs as shown in this link, I see origin = file://
My problem was from this. When I tried without proxy, android device worked correctly but it does not work in the browser.
Oh, chrome://inspect is really great. Thanks for the link!
As it seems, on the device the request is made to the correct domain but CORS prevents it: “Failed to load http://mydomain.ch/abc/data.json: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8080’ is therefore not allowed access.”
In config.xml I have " <access origin="*" />"
Hmm, I’m lost.
Hi Jacktoolsnet, yeah as it seems I’m not able to avoid this, although a React Native App I built uses fetch to retrieve the same file from the same server and does not get stuck… and Ionic Documentation says CORS will not be an issue on the device but in the browser while testing. But anyway, now looking into what I can do server side as it is my hosted server where the file is coming from.
If it’s a CORS problem use HTTP Native, this will automatically fix the CORS problem. You can make some logic to difference if you’re on a browser or device to use either the Native HTTP or Angular’s HttpClient so it won’t be a hell for you to debug.
Or also, you can downgrade from WKWebView to UIWebView, which isn’t really recommended if you’ll release your App for iOS.
on my host I created a php file which reads my json file and sends the data as a response
on my ionic app I do a http get to the php file and retrieve the data - this works in browser (ionic serve) and on devices (Android & iOS)
my php looks like this:
<?php
$response = file_get_contents("./data.json");
header(‘Access-Control-Allow-Origin: *’);
header(‘Content-Type: application/json’);
echo $response;
?>
my ionic provider ts looks like this:
import { Http } from ‘@angular/http’;
import { Injectable } from ‘@angular/core’;
import ‘rxjs/add/operator/map’;
my “view” controller ts looks like this:
//get Data
fetchData() {
this.provider.fetchRemoteData()
.then(data => {
this.memberdata = data; // the data I’m using to show in the view
this.memberdata.sort((a, b) => a.Name.localeCompare(b.Name));
});
}
Considering that mobile apps might want to show some small amount of remote data, without the overkill of a server / database / rest-api this might be a KISS approach.