ALD
ALD

Reputation: 33

Error: Network Error using axios in React Native, issue in only in iOS and Android

Hi to all i've a problem to call apis of my .net-core server from iOS and Android simulators, while from browser it works. In my Backend CORS are enabled:

public void ConfigureServices(IServiceCollection services){
            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

            services.AddMvc();

            services.AddControllers();
        }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
    if (env.IsDevelopment()){
         app.UseDeveloperExceptionPage();
    }
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseCors("MyPolicy");
    app.UseEndpoints(endpoints =>{
       endpoints.MapControllers();
    });
}

The axios GET service:

export function axiosGet():Promise<any>{

  
  const url = "https://localhost:5004/api/Aliments/";
  
  return axios.get(url)
      .then(response => {

          return response.data;
      })
      .catch(error => {
          console.log(error);
      });
};

In your opinion why when i call APIs from IOS/Android simulators return me this error?

Error: Network Error
    at createError (createError.js:16)
    at EventTarget.handleError (xhr.js:84)
    at EventTarget.dispatchEvent (event-target-shim.js:818)
    at EventTarget.setReadyState (XMLHttpRequest.js:600)
    at EventTarget.__didCompleteResponse (XMLHttpRequest.js:395)
    at XMLHttpRequest.js:508
    at RCTDeviceEventEmitter.emit (EventEmitter.js:189)
    at MessageQueue.__callFunction (MessageQueue.js:416)
    at MessageQueue.js:109
    at MessageQueue.__guard (MessageQueue.js:364)

EDIT 1 i've tried to change the host ip in 192.168.1.104(is the lan ip where is hosted my server)

this is my launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59803",
      "sslPort": 44303
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "MyApp",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyApp": {
      "commandName": "Project",
      "launchBrowser": false,
      "launchUrl": "MyApp",
      "applicationUrl": "https://192.168.1.104:5004;",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Upvotes: 0

Views: 6169

Answers (2)

Arsenio Siani
Arsenio Siani

Reputation: 261

You are using https on localhost (pretty sure an auto signed certificate), on a non standard https port. In development (unless special cases) you can use an http protocol. Try to move from https to http, it should woks fine.

Upvotes: 2

Karney.
Karney.

Reputation: 5031

Simulators ip is different from your host machine's ip. Although they are in the same LAN. You can change the ip in this url.

const url = "https://[host ip]:5004/api/Aliments/";

The ip can be queried with: ipconfig in cmd.

Upvotes: 0

Related Questions