1

For a project I am researching postgREST API and applying it to a project being worked on. I simply wanna use the postgREST api in an ajax request from a SpringBoot web app. I am however getting this issue. According to postgREST docs, it will allow ajax requests from any domain. The springboot app is being run on localhost:8080.

Error from get request

     var uri = "localhost:3000/book";
     $.ajax({
          method: "GET",
          url: uri,
          dataType: 'json',
          success: createBook.showResult
     })

The web app is a spring boot app that is using hibernate to persist objects to a postgSQL database and we have build it with CRUD repositories that work fine but our prof wants us to "research" postgREST and implement is partly.

dybka
  • 43
  • 5
  • Does this answer your question? [How to allow all requests in spring boot enabled with CORS?](https://stackoverflow.com/questions/58680107/how-to-allow-all-requests-in-spring-boot-enabled-with-cors) – Maifee Ul Asad Apr 08 '21 at 03:17
  • you have to enable cors in spring boot, here is a question you should take a look at. https://stackoverflow.com/q/58680107/10305444 – Maifee Ul Asad Apr 08 '21 at 03:18

1 Answers1

0

you can add this class to your spring boot project

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final long MAX_AGE_SECS = 3600;

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