I'm trying for a super-simple example here, just to show Ajax working, and I
want to show how to return a structure filled with data results. I think I'm
taking the JSON approach here, and I suspect I'm just missing one step so my
JSON object is being returned as a text string instead of showing up as an
actual object on the other side. Any help is appreciated, thanks!

Here's my tiny web.py app:

import web, json

urls = ( # URLs mapped to handler classes
    '/', 'home',
)

# Where the templates live:
render = web.template.render('templates/')

class home:
    def GET(self):
        return render.displaydata()

    def POST(self):
        input = web.input()
        return json.dumps(dict(   # Sort of seems to do the right thing...
            txt = input.mod.lower(),
            dat = "%.3f" % float(input.num)
            ))

if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()

Here's my coffeescript getdata.coffee which sends requests and displays
results:

getData = ->
  $.post "/",
    { mod: $("#mod").val(), num: Math.random()*Math.PI * 2 },
    (result) ->
      $("#data").html(result).hide().fadeIn(500)
  setTimeout (-> getData()), 2000 # Repeat every 2 seconds

$ ->
  getData()

And here's the HTML page displaydata.html:

<!doctype html>
<html>
    <head>
    <title>Webpy, CoffeeScript and jQuery Ajax</title>
    <script type="text/javascript" src="/static/jquery.js"></script>
    <script type="text/javascript" src="/static/getdata.js"></script>
    </head>
    <body>
      <center>
      <input class="text" id="mod" value="X"
     size="10" style="font-size:2em"/>
     <h1 id="data"></h1>
     </center>
  </body>
</html>


In the CoffeeScript callback function, I'm thinking I should be able to
select the elements of the JSON object by saying "result.txt" and
"result.dat" but that doesn't work, so I'm guessing I haven't really created
a JSON object, just a string. Or I've missed some other step or point.


-- Bruce Eckel
www.Reinventing-Business.com
www.MindviewInc.com

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to