Net :: ERR_CONNECTION_REFUSED ion

I am developing an application using ionic, I can get data from my webApi server and display it in the navigator using ionic serve, but when I try to execute it on the emulator: ionic run android -l -cI get this error:net::ERR_CONNECTION_REFUSED

enter image description here

What is strange is that $ scope contains the necessary data, and I can write it.

+7
source share
4 answers

, Android/ IP / . Localhost, , /. IP- ? - - ?

, Dan

+6

cordova , , -api. -api, . , CORS -.

CORS, Microsoft.AspNet.WebApi.Cors NuGet EnableCors. :

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

, ( ), http://localhost:26309/databases, 26309 itseft, , -. , ERR_CONNECTION_REFUSED, .

http://localhost:26309 IP- DNS-, . http://192.168.8.162:26309 http://yourMachine.yourDomain.com:26309. , -api - IIS Express, . , Visual Studio . IIS . Windows 10 Pro, URL rewrite ARR :

  • IIS, , Windows ., .
  • URL Rewrite 2.0
  • 3.0
  • -. . 26308 ( : IIS Express - 1). .
  • -. "URL Rewrite"
  • "" " ", " "
  • localhost: 26309 " IP-..."
  • ""
  • TCP- 26308 Windows.

cordova, - IIS. http://192.168.8.162:26308 http://yourMachine.yourDomain.com:26308

cordova api -, , . , , USB. Wi-Fi, .

+2

- .

, URL-, , .

, - CORS. Microsoft.AspNet.WebApi.Cors OWIN, startup.cs app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll) ( Microsoft.Owin.Cors)

, , web.config :

<system.webServer>
 <httpProtocol>
      <customHeaders>
        <clear/>
        <add name="Access-Control-Allow-Credentials" value="true"/>
      </customHeaders>
    </httpProtocol>
</system.webServer>

.

0

( Ionic ), http://localhost , . , http://localhost:26309/api/User/getAll/ , .

To solve this problem, you have two options:

The first option is to create a simple proxy in your Ionic app. Edit ionic.config.jsonas follows:

{
  "name": "appname",
  ...
  "proxies": [
    {
      "path": "/localhost",
      "proxyUrl": "http://localhost:26309"
    }
  ]
}

Now that you are making a request from your Ionic app, just write /localhost/api/User/getAll/and it should work.

The second option is to use the private IP address of your computer as the endpoint domain. So instead of using http://localhost:26309you can usehttp://192.168.X.Y:26309

0
source

All Articles