When I post data to the server as JSON via AJAX, values from form elements come is as strings (unicode). Here's a simple example:
<select id="test">
<option value=1>One</option>
<option value=2>Two</option>
<option value=3>Three</option>
</select>
Sending data to the server like this:
$('test').change(function(){
var id = $(this).val();
$.ajax(
{
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: someUrl,
data: JSON.stringify(id:id),
dataType: 'json',
success: doSomething
});
Then on the server (Pyramid 1.3.3):
log.debug(type(request.json_body['id']))
gives <type 'unicode'>
This makes code like if request.json_body['id'] == 2
False since the value is actually u'2'.
I've been doing id = int(request.json_body['id'])
for all the params that should be ints, but is there a better way? I feel like I'm missing something obvious.