Home > Mobile >  I am getting error 0 in Angular app if I enter from different pc in network
I am getting error 0 in Angular app if I enter from different pc in network

Time:10-21

When I launch app from the pc that is hosted I can see the table. If I launch angular app from different pc in lan then I get error 0 in table.

I saw that could be issue with CORS Java Spring Boot but I am new to it. In angular did ng server 192.168.xx --host , i also tried with 0.0.0.0 --host I did build that and tried with http-server but same issue again. Only works in localhost other pc in my lan can't see data, only the frontend.

@Bean
public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://192.168.0.40:4200","http://192.168.0.140:4200","http://192.168.0.31:4200"));
    corsConfiguration.setAllowedHeaders(Arrays.asList("Origin", "Access-Control-Allow-Origin", "Content-Type",
            "Accept", "Jwt-Token", "Authorization", "Origin, Accept", "X-Requested-With",
            "Access-Control-Request-Method", "Access-Control-Request-Headers"));
    corsConfiguration.setExposedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "Jwt-Token", "Authorization",
            "Access-Control-Allow-Origin", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Filename"));
    corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
    urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
    return new CorsFilter(urlBasedCorsConfigurationSource);
}

CodePudding user response:

When you serve with the Angular CLI, you can turn off the CORS piece by adding the --disable-host-check flag to the ng serve command. Try the following:

ng serve --disable-host-check

The documentation for that is HERE.

There is also another option you can look at, to be safer, called --allowed-hosts, which is an array of hosts you allow to connect from. You could also work with that option to help this work.

Best of luck.

CodePudding user response:

Issue is fixed. I did change in angular service from

  private readonly apiUrl = 'http://localhost:8080';

to

private readonly apiUrl = 'http://192.168.0.xxx:8080';
  • Related