0

I'm using Spring 3.2.2 and jQuery.

Everything works fine in local. I made a CORS filter which also works fine.

Every service methods called from another host are working except one. Actually, this is the only method which use @RequestBody and $.toJSON / contentType at client side.

Spring side :

@RequestMapping(value = "/requestOrder", method = RequestMethod.POST)
@ResponseBody
public Object requestOrder(@RequestBody OrderViewModel order) throws NamingException {
    ...
}

jQuery side :

$.ajax({
    type : 'POST',
    url : serviceUrl + 'requestOrder',
    crossDomain : true,
    data : $.toJSON({ ...orderdata... }),
    contentType : 'application/json; charset=UTF-8'
});

Chrome's console log :

XMLHttpRequest cannot load https://REMOTE_HOST/project/service/requestOrder. The request was redirected to 'https://REMOTE_HOST/project/;jsessionid=84781b083f5305ffa22a0adae0a6', which is disallowed for cross-origin requests that require preflight.

HTTP headers :

Request URL:https://.../requestOrder
Request Method:OPTIONS
Status Code:302 Moved Temporarily
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:...
Origin:http://...
Pragma:no-cache
Referer:http://...
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36

I don't see Content-Type which should be set... Request method is OPTIONS ?!

This let me think that the issue is coming from Spring but this is actually working in local...

I would be pleased to get some help.

Ludovic Guillaume
  • 3,237
  • 1
  • 24
  • 41

3 Answers3

0

You are missing the content type mapping. Try this

@RequestMapping(value = "/requestOrder", method = RequestMethod.POST, ,headers="Content-Type=application/json")

For the JQuery part it looks like you need to set both

 contentType: 'application/json; charset=utf-8',
    dataType: 'json',

Look at Send JSON data with jQuery

Community
  • 1
  • 1
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
0

Seens like you need to use JSONP, if this request is beeing made to another domain.

Check here: What is JSONP all about?

Community
  • 1
  • 1
renanleandrof
  • 6,699
  • 9
  • 45
  • 67
0

Found a solution :

I changed the parameter of my Spring mvc method to String and RequestBody to RequestParam :

@RequestMapping(value = "/requestOrder", method = RequestMethod.POST)
@ResponseBody
public Object requestOrder(@RequestParam("order") String orderJson) {

Ajax call is now like this :

$.ajax({
    type : 'POST',
    url : serviceUrl + 'requestOrder',
    crossDomain : true,
    data : { order : $.toJSON({ ...orderdata... }) },
    contentType : 'application/json; charset=UTF-8'
});

And in the requestOrder method, I'm parsing json manually :

ObjectMapper mapper = new ObjectMapper();
OrderViewModel order = mapper.readValue(orderJson, OrderViewModel.class);
Ludovic Guillaume
  • 3,237
  • 1
  • 24
  • 41