0

Calling a java api build with STS and spring MVC from angular in visual studio code, we are getting this error:

Access to XMLHttpRequest at 'http://localhost:8081/HelloWorld/list' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://localhost:8081/HelloWorld/list","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"","xhrStatus":"error"}

gmc
  • 3,910
  • 2
  • 31
  • 44
  • 1
    Possible duplicate of [XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header](https://stackoverflow.com/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header) – JSON Derulo Apr 03 '19 at 10:47

2 Answers2

2

Let's consider your angular application is running at port number 9000. Now, if you want to call your Spring API(that is running at a different port) then you need to add below annotation at controller.

@CrossOrigin(origins = "http://localhost:9000")

Please let me if works for you or otherwise.

Tilak Dewangan
  • 351
  • 1
  • 2
  • 19
0

Spring3CorsFilter.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Spring3CorsFilter {}

Spring3CorsFilterHandlerInterceptor.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class Spring3CorsFilterHandlerInterceptor extends HandlerInterceptorAdapter {


     @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
                Exception {

            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                // Test if the controller-method is annotated with @Spring3CORSFilter
                Spring3CorsFilter filter = handlerMethod.getMethod().getAnnotation(Spring3CorsFilter.class);
                if (filter != null ) {
                    // ... do the filtering
                    response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
                    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                    response.setHeader("Access-Control-Max-Age", "3600");
                    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
                }
            }
            return true;
        }

}

Controller.java

@Spring3CorsFilter
    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.GET,headers="Accept=application/json")
    public List <SuiviCommande> getSearch() int L, @RequestBody SuiviCommandeDto A) {

         ....

    }
khaled
  • 1
  • 1