Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How do I prevent a CORS policy error when posting from JS UI to Java API

My front end application gives this error:

Access to XMLHttpRequest at 'http://localhost:9025/customer/registerDocs/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

when this is called:

updateDocsStatus() {
    return ApiService.postData('http://localhost:9025/customer/registerDocs');
}

This is the controller method in the API:

@PostMapping(value = Constants.REGISTER_DOCS_PATH,
        consumes = {MediaType.APPLICATION_JSON_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseStatus(HttpStatus.OK)
public void registerUserForDocs(HttpServletRequest request){
    String username = securityService.getUsername(request);
    customerHelper.registerCustomerForEdocs(username);
}

I can't help feel like the issue is something on the front end. Am I missing anything obvious here?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you're using Spring Security, CORS can be disabled for all paths (not best practice) by adding the follow bean

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedMethods("*");
    }
}

Or you can fine tune to the POST method and your specific documentation path (customer/registerDocs)

about 4 years ago · Juan Pablo Isaza Report

0

this is what i use, probably best to use this with a specific development profile

@Configuration
public class CorsConfiguration implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/api/**")
            .allowedMethods("*")
            .allowedOrigins("http://localhost:3000")
            .allowedHeaders("*");
}
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!