Home:ALL Converter>net::ERR_CONNECTION_REFUSED ionic

net::ERR_CONNECTION_REFUSED ionic

Ask Time:2016-05-06T07:56:53         Author:Ikram Bk

Json Formatter

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

enter image description here

What is strange is that the $scope contains the required data and I could log them .

Author:Ikram Bk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37062125/neterr-connection-refused-ionic
Dan Bucholtz :

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?\n\nThanks,\nDan",
2016-05-06T03:21:17
Martín De la Fuente :

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. \n\nTo solve this you have two options:\n\nFirst option is to create a simple proxy in your Ionic app. Edit your ionic.config.json as follows:\n\n{\n \"name\": \"appname\",\n ...\n \"proxies\": [\n {\n \"path\": \"/localhost\",\n \"proxyUrl\": \"http://localhost:26309\"\n }\n ]\n}\n\n\nNow, when doing a request from your Ionic app, simply write /localhost/api/User/getAll/ and it should work.\n\nSecond 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",
2019-08-22T05:46:29
Jesús López :

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. \n\nTo enable CORS you need to install Microsoft.AspNet.WebApi.Cors NuGet package and use EnableCors attribute. For example:\n\nusing System.Collections.Generic;\nusing System.Web.Http;\nusing System.Web.Http.Cors;\nusing WebApiServer.Models;\n\nnamespace WebApiServer.Controllers\n{\n [RoutePrefix(\"databases\")]\n [EnableCors(\"*\", \"*\", \"*\")]\n public class DatabasesController : ApiController\n {\n [Route(\"\")]\n [HttpGet]\n public IHttpActionResult Get()\n {\n var databases = new List<DatabaseCategory>()\n {\n new DatabaseCategory\n {\n CategoryName = \"Bases de datos relacionales\",\n Databases = new List<Database>()\n {\n new Database() { DatabaseName = \"SQL Server\" },\n new Database() { DatabaseName = \"Oracle\" },\n new Database { DatabaseName = \"PostgreSQL\" }\n }\n },\n new DatabaseCategory\n {\n CategoryName = \"Bases de datos NoSQL\",\n Databases = new List<Database>()\n {\n new Database() { DatabaseName = \"RavenDB\" },\n new Database { DatabaseName = \"CouchBase\" },\n new Database { DatabaseName = \"MongoDB\" }\n }\n }\n };\n return Ok(databases);\n }\n }\n}\n\n\nIf 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.\n\nSo 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:\n\n\nFirst install IIS, go to control panel, Turn Windows Features On or\nOff, Enable Internet Information Services.\nInstall URL Rewrite 2.0 from here\nInstall Application Request Routing 3.0 from here\nOpen Internet Information Services Manager\nAdd Web Site. Set site name and physical path. On the bindings section, specify 26308 port (my personal convention: IIS Express port - 1). Leave host name blank.\nClick on the newly created web site. On the right pane open \"URL Rewrite\"\nOn the Actions pane click on \"Add Rule\", Select \"Reverse Proxy\"\nWrite localhost:26309 on the text box \"Enter the server name or the IP address...\"\nClick OK\nAdd an exception rule for TCP port 26308 to the Windows Firewall.\n\n\nNow 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\n\nIn 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. \nIt 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.",
2016-05-06T12:24:27
Yevhenii Bahmutskyi :

2020 UPDATE. What worked for me:\nionic cordova run android --no-native-run --livereload --external\n\nAlso adding this to config.xml\n<platform name="android">\n <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">\n <application android:usesCleartextTraffic="true" />\n </edit-config>\n...\n</platform>\n",
2020-07-11T10:21:10
Nsamba Isaac :

2022 Issue : have been also having the same issue and found out that was using ionic cordova run android -livereload and when i normally run my app it was giving me this error:\nnet::ERR_CONNECTION_REFUSED\nwhen i checked my config i find that -livereload Flag added src="http://localhost:8100" to this\n<content original-src="index.html" src="http://localhost:8100" />\ninstead of this :\n<content src="index.html" />",
2022-04-17T11:55:12
yy