JSON data no reaching my view

23 views
Skip to first unread message

Rosciuc Bogdan

unread,
Dec 10, 2014, 7:27:57 AM12/10/14
to pylons-...@googlegroups.com
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


Paul Everitt

unread,
Dec 10, 2014, 8:18:18 AM12/10/14
to pylons-...@googlegroups.com

As a quick suggestion, use Chrome/Firefox to inspect the outgoing request data from your jQuery AJAX call. In Firefox you will need to turn on “Log request/response bodies”. I think you’ll find that you’re not actually sending the right thing.

Also, try using the pyramid_debugtoolbar to inspect what is coming in.

—Paul

--
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 pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Wichert Akkerman

unread,
Dec 10, 2014, 9:31:16 AM12/10/14
to pylons-...@googlegroups.com
On 10 Dec 2014, at 13:27, Rosciuc Bogdan <rosciuc...@gmail.com> wrote:

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.

You need to use request.json_body to get decoded JSON data.

Wichert.

Rosciuc Bogdan

unread,
Dec 10, 2014, 10:31:38 AM12/10/14
to pylons-...@googlegroups.com

I did use the request.json_body but it gives back an error. The value dosen't reach the view...

--
You received this message because you are subscribed to a topic in the Google Groups "pylons-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pylons-discuss/xSJUW4Bw4_0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pylons-discus...@googlegroups.com.

Michael Merickel

unread,
Dec 10, 2014, 10:34:38 AM12/10/14
to Pylons
Can you intercept the request in your browser and see if the
content-type and body are actually correct? request.json_body only
works on application/json -ish content types by default.
> 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
Reply all
Reply to author
Forward
0 new messages