JSON getting the series data into view

45 views
Skip to first unread message

Aydin

unread,
Sep 12, 2016, 9:46:52 PM9/12/16
to web2py-users
The methods to get the data in the view of web2py does not seem working. I used the method mentioned in http://web2py.com/books/default/chapter/29/10/services which is

<script>
        $.getJSON('/application/default/weekdays',
          function(data){series: [{
                type: 'area',
                name: 'Response(kW)',
                data: data
            }] 
    });
for plotting purpose and it did not work; even the plot did not show up. I also used the method inhttp://www.web2pyslices.com/slice/show/1334/consuming-a-web2py-json-service-with-jquery that basically say

jQuery.getJSON("{{=URL(r=request,f='call',args=['json','get_days'])}}",
        function(data){...});
My controller:

def getdata():
    rows = db().select(db.test.id, db.test.x, orderby=db.test.id)
    return json.dumps([[r.id, r.x] for r in rows])


Niphlod

unread,
Sep 13, 2016, 5:46:32 AM9/13/16
to web2py-users
did you check if your js is working before blaming web2py ?

Aydin

unread,
Sep 13, 2016, 7:39:19 AM9/13/16
to web2py-users
Im sure it has to do with my code not web2py.
Yes in the browser i did /app/default/getdata and it returned correctly [[1,2],[2,3]].
By the way i import json and also use @service.json.

Niphlod

unread,
Sep 13, 2016, 10:05:58 AM9/13/16
to web2py-users
then please check with whoever wrote 

"""
function(data){series: [{
                type: 'area',
                name: 'Response(kW)',
                data: data
            }] 
"""

PS: may I suggest that you'd need to call /app/default/getdata.json that sets automatically also the correct content-type for json ?

Brian Boatright

unread,
Sep 13, 2016, 10:07:21 AM9/13/16
to web2py-users
Make sure you are using the correct URL to retrieve the data. 

I just did a test of that example from the book by creating a controller and index view and it works as expected. 

In your original post you show
$.getJSON('/application/default/weekdays',

and in your last reply you suggest you tested it by viewing in browser
/app/default/getdata

those are very different obviously but without further details I will assume you are using one or the other. 

/application needs to be the name of your actual application it could be "examples", "welcome", or some custom name you gave the application. 


Aydin

unread,
Sep 13, 2016, 11:38:11 AM9/13/16
to web...@googlegroups.com
@Niphlod and Brian:
Sorry that was my mistake, I mixed up the codes.
the actual code was:

<script>
        $.getJSON('/application/default/getdata',

          function(data){series: [{
                type: 'area',

                name: 'x',
                data: data
            }] 
    });

So since it looked like there was no error in the procedure, I played with the URL argument a bit and what worked for me was /default/getdata. I don't know why when I added application name it was not working. 
I confirmed that it works with both  /default/getdata and  /default/getdata.json. 
Anyways, it seems that the application name should not be in the url argument. 

Niphlod

unread,
Sep 13, 2016, 2:59:26 PM9/13/16
to web2py-users
definitely something is going on with your setup: did you play with routes ? 

the default scheme includes the app name right after the host name, and that's why you got the example with /app/controller/function.

Aydin

unread,
Sep 13, 2016, 3:27:22 PM9/13/16
to web2py-users
Yes, thats the reason i think. Thanks!
Reply all
Reply to author
Forward
0 new messages