$(".submit").click(function (e) {
e.preventDefault();
$.ajax({
async: true,
crossDomain: true,
method: "post",
data: $("form[name=something]").serialize(),
url: "myUrl",
success: function (res) {
console.log(res);
},
error: function (res) {
console.log(res.status);
},
});
});
and my spring mvc controller like
@RestController
@CrossOrigin(origins = {"*"},
methods = {RequestMethod.GET, RequestMethod.POST,
RequestMethod.PUT, RequestMethod.DELETE})
public class AppController {
...
...
...
}
If is send (get) request it work properly but when i send (post) it got exception : Access to XMLHttpRequest at (remote server)from origin (localhost)has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Make sure the CORS policy is opened at the remote domain. if not, you will get the same error. Also if you are using Spring and created/extended a WebSecurityConfigurerAdapter file, you can configure the CORS by redefining CorsConfigurationSource bean in the WebSecurityConfig file. just make sure to add your localhost url in allowed origins.
Something like below:
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("..your localhost url.."));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}