I'm developing an application using ionic , I could retrieve data from my webApi server and display them on the navigator with ionic serve
but when I try to execute it on the emulator:ionic run android -l -c
i get this error :net::ERR_CONNECTION_REFUSED
What is strange is that the $scope contains the required data and I could log them .
More than likely localhost on the Android device/emulator does not resolve to the IP of your development machine/server. Localhost probably points to the device/emulator itself. Can you hit the actual IP Address of your machine instead? Or can you host the web service somewhere in the cloud and try that?
Thanks, Dan
Since the cordova app is loaded from the device file system, it has a different origin than the web api. The app tries to call the web api, but this is prohibited by the Same Origin Policy. To overcome this issue you need to enable CORS on your web api.
To enable CORS you need to install Microsoft.AspNet.WebApi.Cors
NuGet package and use EnableCors
attribute. For example:
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Cors;
using WebApiServer.Models;
namespace WebApiServer.Controllers
{
[RoutePrefix("databases")]
[EnableCors("*", "*", "*")]
public class DatabasesController : ApiController
{
[Route("")]
[HttpGet]
public IHttpActionResult Get()
{
var databases = new List<DatabaseCategory>()
{
new DatabaseCategory
{
CategoryName = "Bases de datos relacionales",
Databases = new List<Database>()
{
new Database() { DatabaseName = "SQL Server" },
new Database() { DatabaseName = "Oracle" },
new Database { DatabaseName = "PostgreSQL" }
}
},
new DatabaseCategory
{
CategoryName = "Bases de datos NoSQL",
Databases = new List<Database>()
{
new Database() { DatabaseName = "RavenDB" },
new Database { DatabaseName = "CouchBase" },
new Database { DatabaseName = "MongoDB" }
}
}
};
return Ok(databases);
}
}
}
If the app running on a device (emulated or physical) calls http://localhost:26309/databases it tries to connect to port 26309 on the device itseft, not to the developer machine that is running the web api. So you receive ERR_CONNECTION_REFUSED
, because no one on the device is listening on that port.
So instead of using http://localhost:26309
you should be using your developer machine ip address or a DNS name that resolves to it. For example http://192.168.8.162:26309
or http://yourMachine.yourDomain.com:26309
. But then you will hit another problem, your web api is running on the development web server IIS Express which doesn't allow remote connections. It can be configured to allow remote connections, but then you would have to execute Visual Studio as administrator. A better solution is installing IIS and configuring it to behave as reverse proxy. This can be done in Windows 10 Pro by installing URL rewrite and ARR, and configuring it:
Now you need to modify you cordova app to make requests to the newly created web site on IIS. It needs to make requests to http://192.168.8.162:26308
or http://yourMachine.yourDomain.com:26308
In this way you can debug your cordova app and the web api app at the same time, I mean you can add break points to both. It works for emulated devices as well as physical devices attached to USB. You will need a proper wifi accessible by your development machine and device.
From your emulator (and the Ionic app is running in the emulator), http://localhost
no longer refers to your computer, it refers to the emulated devise. Understanding this, http://localhost:26309/api/User/getAll/
is not a valid endpoint in your devise so the request fails.
To solve this you have two options:
First option is to create a simple proxy in your Ionic app. Edit your ionic.config.json
as follows:
{
"name": "appname",
...
"proxies": [
{
"path": "/localhost",
"proxyUrl": "http://localhost:26309"
}
]
}
Now, when doing a request from your Ionic app, simply write /localhost/api/User/getAll/
and it should work.
Second option is to use the private IP of your computer as the endpoint domain. So instead of using http://localhost:26309
you can use http://192.168.X.Y:26309
2020 UPDATE. What worked for me:
ionic cordova run android --no-native-run --livereload --external
Also adding this to config.xml
<platform name="android">
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:usesCleartextTraffic="true" />
</edit-config>
...
</platform>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With