Get return value from controller function via ajax

414 views
Skip to first unread message

Ian W. Scott

unread,
Jan 20, 2016, 2:45:55 PM1/20/16
to web2py-users
I'm trying to use ajax calls to update the data for a chart dynamically. I don't want to make the chart a component that refreshes, I just want to get the controller return value back to the javascript in the view, so that I can update the chart via javascript. But web2py's ajax function seems to only (a) update a part of the page html, or (b) send page data to the controller for use in the back-end. There doesn't seem to be any way to get the controller's return value as a data object for the javascript to use.

I've tried just using jquery's get() method like this:

    $.get(my_controller_url, function(data){
        console.log('got ajax data', data);
    });

But if the controller's return value is a tuple there seems to be no data sent back, or at least I don't know how to access it from the 'data' variable in this example. If I make the controller a dictionary I get html back. But I don't want html. I just want the data.

So how can I do this?

Niphlod

unread,
Jan 20, 2016, 2:50:35 PM1/20/16
to web2py-users
from gluon.serializers import json

def uh():
    return json(blablabla)

Richard Vézina

unread,
Jan 20, 2016, 3:36:01 PM1/20/16
to web2py-users
Simone,

Since simplejson have be exit, and standard lib json it reputed multifold time slower what are you suggesting as a remedy?

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niphlod

unread,
Jan 20, 2016, 3:43:38 PM1/20/16
to web2py-users
ehm...... what again ?

Richard Vézina

unread,
Jan 20, 2016, 3:49:58 PM1/20/16
to web2py-users
again what, off topic, ever!!

:)

Richard

Niphlod

unread,
Jan 20, 2016, 3:59:45 PM1/20/16
to web2py-users
I really don't know what you meant (ontopic or offtopic)

Richard Vézina

unread,
Jan 20, 2016, 4:04:33 PM1/20/16
to web2py-users
I mean, before in gluon/contrib there were simplejson, but I notice it had been removed in 2.10.x if I remember because it was not py3 compliant... I discover it hadly when my production app shown some strange behavior in some search field or autocomplete field where I was using it...

I did't replace it with ujson which is really fast, but I would like to know what you propose instead of plain json from standard library?

Richard

Richard Vézina

unread,
Jan 20, 2016, 4:05:17 PM1/20/16
to web2py-users
To my knowlege simplejson contrib was there because of standard json lib was too slow...

Richard

Niphlod

unread,
Jan 20, 2016, 4:16:01 PM1/20/16
to web2py-users

Ian W. Scott

unread,
Jan 20, 2016, 4:20:22 PM1/20/16
to web2py-users
Thanks. So to clarify, if I serialize the return value as json in the controller, then I can use it in javascript. It looks like I still have to parse the json on the javascript end (like $.parseJSON(mydata)) but it works like a charm.

Ian

Richard Vézina

unread,
Jan 20, 2016, 4:23:00 PM1/20/16
to web2py-users
https://groups.google.com/d/msg/web2py-developers/2U-4hnwjc60/9LvGVn_P2VYJ

You are right I am having it... I guess I shouldn't believe everything what I read...

:)

Thanks to clarify that... I should check my code since the issue I was having get solved by another concurrent change which may was the only thing causing the issue... And that explain why I didn't have any import issue

Richard

Richard Vézina

unread,
Jan 20, 2016, 4:23:40 PM1/20/16
to web2py-users
Yes always better to parse it...

--

Niphlod

unread,
Jan 20, 2016, 4:34:53 PM1/20/16
to web2py-users
the deal is basically that if you return a dict, web2py is compelled to pass it to the corresponding view, which in turns in most of cases returns html.
There's nothing wrong in returning a dict and then having a generic "json" view that serializes that dict, and as a matter of fact, generic.json does just that.
But if you return a string, you skip the "render the view" part.

On the parsing side: if you are looking for json, you should also return the correct content-type if you want libraries (e.g. jQuery) to parse for themselves. Either you force it with response.headers['Content-Type'] = 'application/json' or - my personal preference - you just ask for /a/c/f.json instead of /a/c/f . the correct content type will be set by web2py.

Anthony

unread,
Jan 20, 2016, 4:45:49 PM1/20/16
to web2py-users
Additionally, if you still want to use the ajax() function, the third argument can be ":eval", in which case, you can return some Javascript code to be executed when the response is returned. Alternatively, the third argument can be an actual Javascript function, in which case, the returned value will be passed to that function.

Anthony

Ian W. Scott

unread,
Jan 21, 2016, 2:37:19 PM1/21/16
to web...@googlegroups.com
Ah, thanks Anthony. I didn't realize that the third argument in the ajax() function was that flexible. That helps.

Ian

--
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/4gSYo-Th4vw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Ian W. Scott, PhD (McMaster RS)
Associate Professor of New Testament
Tyndale Seminary
Toronto, Ontario, Canada

Paul's Way of Knowing: Story, Experience and the Spirit (WUNT II/205; Tübingen: Mohr Siebeck, 2006/Grand Rapids: Baker Academic, 2008).

The Online Critical Pseudepigrapha
(Atlanta: Society of Biblical Literature, 2006-). Online: http://www.purl.org/net/ocp.
Reply all
Reply to author
Forward
0 new messages