menu

Questions & Answers

Ionic calls to backend not working on smartphone

Calls from my ionic app to my backend don't work on the smartphone. They work in the browser and in the emulator, but when I install the apk on the smartphone, it doesn't work.

My frontend is in ionic with capacitor.

My backend is java with Springboot.

This is my backend call:

  url = `${SERVER_URL}/recipe`;

  constructor(private httpClient: HttpClient, private alertController: AlertController) { }
  
  findByIngredients(urlIds): Observable<Recipe[]> {
    return this.httpClient.get<Recipe[]>(this.url + urlIds)
      .pipe(
        retry(2),
        catchError(this.handleError))
  }

  findLastTen(): Observable<Recipe[]> {
    return this.httpClient.get<Recipe[]>(`${this.url}/lastTen`)
      .pipe(
        retry(2),
        catchError(this.handleError))
  }

This is my backend cors configuration:

@Configuration
public class CorsConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "TRACE");
    }
}

This is one of my endpoints:

@RestController
@RequestMapping("/recipe")
public class RecipeController {

    @Autowired
    private RecipeFacade facade;

    @GetMapping(value = "/lastTen")
    public ResponseEntity<List<GetAllRecipesDTO>> getLastTenRecipes() {
        List<GetAllRecipesDTO> dto = facade.getLastTenRecipes();
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

Error message that is returned:

http failure response for http://...:9000/recipe/lastTen: 0 Unknown Error

Comments:
2023-01-20 19:42:02
Why didn't you post any errors or describe what "doesn't work" measn? Why did you tag this cors? Do you believe cors is the problem?
2023-01-20 19:42:02
When I say it doesn't work, it's because the call to the backend doesn't work on the smartphone. I've updated the post with the error message that is returned. I marked it with cors too, because I don't know if the problem is in cors, or if it's in the frontend call.
Answers(0) :