Hello all,
I'm trying to build a page where when the user presses a button a variable
which initially is 0 increments with 1. This number is then sent
asynchronously to the server by using Jquery AJAX.
What I have so far is:
*In my __init__.py file:*
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind = engine)
Base.metadata.bind = engine
config = Configurator(settings = settings)
config.include('pyramid_jinja2')
config.add_static_view('static', 'static')
config.add_static_view('scripts', 'scripts')
*#Removed the other views*
config.add_route("declare_usage",
'/user/{user_id}/{address_id}/declare')
config.add_route("declare_usage_json",'/user/{user_id}/{address_id}/declare.json')
config.scan()
*My HTML + Jinja2:*
*#Removed code for simplicity*
<div id="button_add">Add</div>
{{val}}
*My JS:*
$(document).ready(function(){
var room = 0;
jQuery.ajax({type:'POST',
url: '/user/1/5/declare', *#I use a direct user ID and a direct address ID
as I'm not sure how to send this to JS from Pyramid ... yet :).*
data: JSON.stringify(room),
contentType: 'application/json; charset=utf-8'});
$('#button_add').click(function(){
room = room + 1;
});
});
*My view code:*
@view_config(route_name = 'declare_usage', renderer = 'declara.jinja2')
@view_config(route_name = 'declare_usage_json', renderer = 'json')
def declara_consum(request):
*#Removed code for simplicity*
val = request.POST.get('room')* #I get a "None value in my html" if I
change to request.json_body -> I get an error that there is no json to be
parsed.*
return { 'val' : val }
What happens is that when I open the debugger the POST request is
successful with no data and on the page I get 2 options for 'val':
1. None -> when i use val = request.POST.get('room')
2. Error ''ValueError: No JSON object could be decoded" -> when I use val =
request.json_body
Also, still can't get it to work if in my JS i change url to be
''/user/1/5/declare.json' and/or data to {'room' : room}
Can somebody please point out what I'm doing wrong?
Many thanks,
Bogdan
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.