0

On the server side, I have a method which looks like this:

@POST
@Path("/foods/{year}/{month}/{day}")
@Consumes("multipart/mixed")
@Produces("application/json; charset=UTF-8")
@Transactional
public boolean setFoodsForTheDay(@PathParam("year") int year, @PathParam("month") int month,
  @PathParam("day") int day, @Multipart(value = "foodList", type = MediaType.APPLICATION_JSON) Food[] foodList) {

  if (log.isDebugEnabled()) {
    log.debug("list size={}", foodList.size());
  }
  doStuff(foodList);

}

If I send in the following POST request to /foods/2013/06/26 it will actually work and the array will get parsed correctly:

Host: localhost:7777
Accept: application/json
Content-Type: multipart/mixed; boundary=---------------------------25819220131967
Content-Length: 226

-----------------------------25819220131967\r\n
Content-Disposition: form-data; name="foodList"\r\n
Content-Type: application/json\r\n
\r\n
[   {"id":null,"name":"Banana","recipe":null}   ]\r\n
-----------------------------25819220131967--\r\n

As you can see, it's important to send in a multipart/mixed (or perhaps multipart/form-data would work too) because then I can set the Content-Type of the part, and it will get parsed correctly.

This all works. Now the problem is, I need to send this request in with jQuery (or any other Ajax tool) and looks like it's not possible to send multipart/mixed? Or there's some trick with an iframe but it will still not be possible to set the Content-type of the part.

Does anyone know of a solution for this problem? How can I send in an array of objects to the server in JSON serialization?

2 Answers 2

3

Looks like this is not possibly with jQuery, however I did find a blog which shows how to do this with the old XMLHttpRequest.

This is my Javascript code now, it works perfectly! :)

function sendFoodRequest() {
    var xhr = new XMLHttpRequest();

    xhr.open("POST", 'http://localhost:7777/services/rest/foods/2013/06/25', true);

    var boundary = '---------------------------';
    boundary += Math.floor(Math.random()*32768);
    boundary += Math.floor(Math.random()*32768);
    boundary += Math.floor(Math.random()*32768);
    xhr.setRequestHeader("Content-Type", 'multipart/mixed; boundary=' + boundary);
    var body = '';
    body += '--' + boundary + '\r\n' + 'Content-Disposition: form-data; name="foodList"' + '\r\n';
    body += "Content-Type: application/json\r\n\r\n";
    body += '[   {"id":null,"name":"Spinach","recipe":null}   ]';
    body += '\r\n'
    body += '--' + boundary + '--';
    xhr.setRequestHeader('Content-length', body.length);
    xhr.onload = function() { }
    xhr.send(body);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes you can send multipart/mixed through JQuery ajax, but must add extra stuff:

cache: false,
contentType: false,
processData: false,


$.ajax({
    url: 'php/test.php',
    data: {name: "test1", age 5},
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

1 Comment

That doesn't seem to work. Under Chrome I get "Invalid media type" error, while under Firefox a really long error which goes like "JavaScript component does not have a method named: "available"' when calling method..." As far as I can see that would be an expected error if I was trying to send, for example, a DELETE request, and my brower was not supporting that... however, in this case, I'm only sending a typical POST request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.