tags:

views:

65

answers:

2

When I send a JSON representation of my entity to the server, how should the payload be delivered ? As far as I see, there are two options:

  1. as an uploaded file
  2. as the content of a POST form data

The first case cannot be performed via AJAX request, as far as I see, but it seems the "right one". Do you know if there's a preferred "de facto" way to perform this task?

A: 

You can use the POST method to send whatever data you want, which means, you can load it (the body of the request) with JSON as well and it's perfectly correct. Just make sure your server application is able to decode it - for this you could provide your request with a correct Content-Type header ("application/json" or whatever is the right media type), so the server side knows how to deserialize the payload.

Milan Novota
A: 

You should have no problem posting a JSON entity directly from Javascript. See here for examples on how to do it with jQuery.

I see no advantage to using a Form to post the data.

Darrel Miller
What I mean is that Ajax.post accepts data as key/value pairs (serialized or not). This means that these data will appear in request.POST in django, for example, not in request.FILES. In order to set up a file upload it's a much more complex issue.
Stefano Borini
It weird how often frameworks seem to make it more difficult to work with HTTP than accessing it directly.
Darrel Miller