3

This is quite an old question but after trying everything I learned, I still can't get it to function properly after hours of trying to send JSON data over CORS POST request. GET and POST with parameters worked perfectly but POST data seems to be elusive.

You won't see it in the code here, but CORS filter-mapping is set up for all servlets. I don't think it's the reason why I can't hit the controller methods from ajax request.

Environment:

  • jquery: 1.8.2
  • chrome: Version 23.0.1271.64 m
  • Spring MVC 3.0.5.RELEASE

I attempted it with httprequest software, so I know if I can send following request to the server, it would work properly. But I just can't send a request over with Content-type other than the default, so I never actually send the real request after pre-flight

Required Request to get proper response from server:

POST http://localhost:8068/web-app/app/api/post-json-obj
Content-Type: application/json
{"description": "hello", "title": "JohnDoe", "identifier":"testResponse"}

Code:

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script type="application/javascript">   
//Required Request Info:
//POST http://localhost:8068/web-app/app/api/post-json-obj
//Content-Type: application/json
//{"description": "hello", "title": "JohnDoe", "identifier":"testResponse"}

(function($) {
var url = 'http://localhost:8068/web-app/app/api/post-json-obj';
var data = {"description": "hello", "title": "JohnDoe", "identifier":"testResponse"};
$.ajax({
type: 'POST',
url: url,
data: JSON.stringify(data),
contentType:"application/json",
success: function(response) {
alert("success");
},
error: function(xhr) {
alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
})(jQuery);
</script>
</head>
<body>

</body>
</html>

Server-side:

  @RequestMapping(value="/post-json-obj", method = RequestMethod.POST, consumes = {"application/json"})
      public @ResponseBody ResponseEntity<WebResponse> postJSONObject(@RequestBody Item item) {
          WebResponse res = new WebResponse();
          res.setMessage("success");    
  return new ResponseEntity<WebResponse>(res, HttpStatus.ACCEPTED);
  }

Pre-flight:

Request

OPTIONS /web-app/app/api/post-json-obj HTTP/1.1
Host: localhost:8068
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Pragma: no-cache
Cache-Control: no-cache

Response

  HTTP/1.1 200 OK
  Server: Apache-Coyote/1.1
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Methods: GET, POST, PUT, DELETE
  Access-Control-Allow-Headers: Content-Type
  Access-Control-Max-Age: 1800
  Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
  Content-Length: 0
  Date: Wed, 14 Nov 2012 20:27:37 GMT

Real request

Request:

  POST http://localhost:8068/web-app/app/api/post-json-obj HTTP/1.1

Request Payload: {"name":"hello","message":"JohnDoe"}

Response:

No response

ttback
  • 2,051
  • 5
  • 27
  • 40
  • You showed only request, not responce. And add _Item_ source – vacuum Nov 13 '12 at 22:00
  • Possibly related? http://stackoverflow.com/a/5589498/710446. Your server should serve a `Access-Control-Allow-Headers: Content-type` header. – apsillers Nov 13 '12 at 22:45
  • @apsillers Have that in the CORS filter class, but somehow it is not showing up. When it succeeds for GET and POST with params, I do see that header. This one just looks like it's done after getting the OPTIONS request, and didn't actually execute the real request. – ttback Nov 13 '12 at 22:56
  • @vacuum I'll try to add that tomorrow, but that is really just an entity object with those fields. The response was there, now labeled. – ttback Nov 13 '12 at 22:56
  • I have tried everything around the headers etc, since other form of requests worked properly, I feel like it may be related to the jquery ajax request part. It's also possible that jQuery ninja-ed in some header without my knowledge. – ttback Nov 13 '12 at 22:59
  • I just answered a similar question here: http://stackoverflow.com/questions/11096195/spring-and-http-options-request/16021886#16021886 it might be useful if this is still a problem – Jim Apr 15 '13 at 18:12

0 Answers0