0

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"
      }
    }
  }
}
2
  • Is your simulator connected to the internet? Commented Dec 23, 2020 at 17:14
  • Simulators are connected in Lan mode, because i'm using expo. Commented Dec 23, 2020 at 21:51

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, now it works fine in localhost
0

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.

7 Comments

that url is the server address
@ALD, you use localhost, localhost is the simulator address. So you can change it to a specific ip, such as: 1.1.2.3:5000.
Remove services.AddMvc();, because it is duplicated with services.AddControllers().
i've use se follow url = "192.168.1.104:5004/api/Aliments" and changed the my launchSettings.json in the BE. I've just posted launchSettings.json in main post
Do you change the url in axiosGet? const url = "https://192.168.1.104:5004/api/Aliments/"; ` launchSettings.json` needn't to be changed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.