{"id":30091,"date":"2015-11-18T00:48:39","date_gmt":"2015-11-17T19:18:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=30091"},"modified":"2015-11-19T13:25:31","modified_gmt":"2015-11-19T07:55:31","slug":"how-to-allow-cross-domain-ajax-calls-to-an-application-in-spring-boot-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-allow-cross-domain-ajax-calls-to-an-application-in-spring-boot-application\/","title":{"rendered":"How to allow Cross Domain Ajax calls to an Application in Spring Boot Application"},"content":{"rendered":"<p>You can allow Cross Domain Ajax calls to an application by just registering a new filter and then configure it to <strong>Allow-Origin : {your domain&#8217;s}<\/strong> or you can use a <strong>wild card &#8220;*&#8221;\u00a0<\/strong> to allow the calls from all domains.<\/p>\n<p>You can even Define the Custom Headers your application supports by defining them in a comma separated format , e.g\u00a0<strong>Access-Control-Allow-Headers: &#8220;header-1,header-2,header3&#8221;<\/strong><\/p>\n<pre>package com.ekiras.filter;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\nimport javax.servlet.*;\r\nimport javax.servlet.http.HttpServletResponse;\r\nimport java.io.IOException;\r\n\r\n@Component\r\npublic class CorsFilter implements Filter {\r\n\r\n    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {\r\n        HttpServletResponse response = (HttpServletResponse) res;\r\n        response.setHeader(\"Access-Control-Allow-Origin\", \"*\");\r\n        response.setHeader(\"Access-Control-Allow-Methods\", \"POST, GET, OPTIONS, DELETE\");\r\n        response.setHeader(\"Access-Control-Max-Age\", \"3600\");\r\n        response.setHeader(\"Access-Control-Allow-Headers\", \"Content-Type, x-requested-with, X-Custom-Header\");\r\n        chain.doFilter(req, res);\r\n    }\r\n\r\n    public void init(FilterConfig filterConfig) {}\r\n\r\n    public void destroy() {}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can allow Cross Domain Ajax calls to an application by just registering a new filter and then configure it to Allow-Origin : {your domain&#8217;s} or you can use a wild card &#8220;*&#8221;\u00a0 to allow the calls from all domains. You can even Define the Custom Headers your application supports by defining them in a [&hellip;]<\/p>\n","protected":false},"author":173,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[446,1],"tags":[96,2763,2072],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30091"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/173"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=30091"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30091\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=30091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=30091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=30091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}