Using CORS endpoints in application.properties instead of @CrossOrigin

4.5k views Asked by At

I am using Spring boot with gradle build and have added the below properties in application.properties.

endpoints.cors.allowed-origins=* 
endpoints.cors.allowed-methods=*

But when I do AJAX call from another domain, it is giving error as below:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9000/hello-world. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Firefox: Host
localhost:9000 Origin
localhost:9002 Referer localhost:9002/hello

build.gradle has the dependencies as:

compile 'org.springframework.boot:spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-starter-actuator")

Controller:9000

@Controller
@RequestMapping("/hello-world")
public class HelloWorldController {
    @RequestMapping(method=RequestMethod.GET)
     public @ResponseBody Greeting sayHello(@RequestParam(value="name", required=false) String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

}

AJAX

$(document).ready(function() {
    alert("hii")
    $.ajax({
        url: "http://localhost:9000/hello-world"
    }).then(function(data, status, jqxhr) {
       $('.greeting-id').append(data.id);
       $('.greeting-content').append(data.content);
       console.log(jqxhr);
    });
});
0

There are 0 answers