So I have this code:
var ajaxUrl = 'a/7776/as';
var data = {
'answer' : { 'user_input' : [] },
'form_build_id' : 'form-ffe6f10e9601470ed4cfe38257a959a6'
}
$.ajax({
url: ajaxUrl,
dataType: 'json',
type: 'POST',
data: data,
success: function(json){
}
});
However when I inspected the POST source it appears that 'answer' is not being sent..
Here's the POST source in Firebug:
Parametersapplication/x-www-form-urlencoded
form_build_id form-ffe6f10e9601470ed4cfe38257a959a6
Source
form_build_id=form-ffe6f10e9601470ed4cfe38257a959a6
Why is this the case and how can I get it to also send the 'answer' part of the object into the AJAX post?
The default POST
encoding is multipart/form-data
, which consists of a flat series of name=value
pairs.
Your structure cannot be mapped directly to a flat series of name=value
pairs, because it's complex. You have a member which is an object, which has a property referring to an array.
If you want to send an arbitrarily-complex structure, you have to use a different encoding and ensure that the server understands that encoding. For instance, you can send JSON or XML to the server, but you have to tell it that's what you're doing by setting the contentType
property on the ajax
method (which sets the type of the data you're sending to the server). Then your server has to understand how to deserialize that JSON or XML.
Alterately, ensure that your structure can be mapped to a flat series of name=value
pairs.
A half-way house is to send data as multipart/form-data
, but where you send a single name=value
pair and have the value
part be a different encoding, like this:
$.ajax({
url: ajaxUrl,
dataType: 'json',
type: 'POST',
data: {json: JSON.stringify(data)},
success: function(json){
}
});
That sends a single name=value
pair with the name json
, where the value is a JSON-encoded string. Your server-side would retrieve the value of the json
parameter in the normal way, and then use a JSON deserializer to recreate the object graph.
This half-way-house technique is sometimes handy in frameworks that make it difficult to use something other than standard request encoding.
Because you cannot post a zero length array. POST (or HTTP query) keys do not carry any type information and zero length array would effectively build into null in a POST body: message = [] will serialize into ''.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With