Skip to content Skip to sidebar Skip to footer

Spring RestTemplate Call To API Worked But JQuery Failed Because Same-origin Policy

When I use Spring RestTemplate to call a Rest API. public class JiraBusImpl implements JiraBus { private RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers =

Solution 1:

You can use @CrossOrigin() annotation.

https://spring.io/guides/gs/rest-service-cors/

Or just add a filter to set response header to allow cross origin.

'Access-Control-Allow-Origin' error in Spring MVC + Zepto POST


In your jQuery ajax call, change it to dataType: "jsonp" to enable the cross origin.


Sorry for my misunderstanding. Same-origin policy is applied on web browser. https://en.wikipedia.org/wiki/Same-origin_policy

You can even turn it off. Disable same origin policy in Chrome

So that means at the first place, a rest service can be accessed by any clients if only the server side allows it. However, for safety issues, web browsers disable it. So it's not the problem how RestTemplate does it. It's because that web browser disable it.


Post a Comment for "Spring RestTemplate Call To API Worked But JQuery Failed Because Same-origin Policy"