Assume we use jQuery.ajax() to POST data with two parameters, game_id and player_id.
When we use jQuery.ajax(), the server receives parameters like this:{"_json"=>"game_id=4f6a593a8cb45b16c0000491&player_id=4f68ed4b8cb45b16c0000111"}
We would like the server to receive parameters like this:
{"game_id=4f6a593a8cb45b16c0000491&player_id=4f68ed4b8cb45b16c0000111"}
Essentially, ajax() makes “_json” the master key for all parameters. Is there a way to prevent this, or are we doing something wrong?
Here’s some specific code:
$.ajax({
type: 'POST',
url: UPDATE_GAME_URL,
data: { "game_id" : game_id,
"player_id" : get_player_id(),
"turn_set" : JSON.stringify(turn_set) },
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
Thanks!
jQuery does not contain any code that prefixes fields with
json_so the problem is somewhere else.However, you need to remove
contentType: 'application/json; charset=utf-8'to ensure the server parses the POST data correctly – you are not posting JSON after all.If your server does expect a JSON payload (according to the string you expect to receive it doesn’t!), you would have to use
data: JSON.stringify({...})to ensure you actually send a JSON string instead of form-encoded key/value pairs.