Async help, loadJSONdoc and evalJSONrequest

15 views
Skip to first unread message

Adam Yee

unread,
Jun 17, 2009, 5:48:40 PM6/17/09
to MochiKit
Hi all, I'm a newbie to MochiKit and JS, so bare with me if there are
syntax errors or design flaws.

I'm trying to achieve simple AJAX functionality (using JSON) by
submitting a single form input and displaying the input asynchronously
with .innerHTML.

My code (testpage.html and controllers.py):
http://dpaste.com/hold/56577/

The connect('form', 'onsubmit', submitClicked) doesn't seem to be
doing anything. The json.dumps(d) is doing its job - when submit is
clicked, the page refreshes with only {"insert": "whatever text
here"}. I can't figure out how to json.loads properly, but if I can't
get the JS to work, the the server side script won't receive any
json. So my struggle is in sending the xmlhttprequest.

Thank you for helping,
Adam

Imri Goldberg

unread,
Jun 18, 2009, 8:44:24 AM6/18/09
to Adam Yee, MochiKit
Hi
Try to do the connect after the page has loaded. Use addLoadEvent for that.
So your code will look something like that:

function bla() {
    connect(...);
}

addLoadEvent(bla);

Some other issues:
If you are using ajax, you don't really need form actions, the clicking submit will do a regular (non-ajax) POST.
I usually connect to onclick if I want ajax.

It seems that you are using turbogears 2. I'm using turbogears1, so take my advice with a grain of salt, but in tg1 you can do expose("json"), and return a regular dict.

If you need further help with that, I'll be glad to help you off-list.

Cheers,
Imri
--
Imri Goldberg
--------------------------------------
www.algorithm.co.il/blogs/
--------------------------------------
-- insert signature here ----
Reply all
Reply to author
Forward
0 new messages