Search code examples
jquerypythoncherrypy

$post and Cherrypy


I'm currently working on a project where we previously used Django. However this proved a bit to heavyweight for our needs so we are shifting the project over to use cherrypy as we only really need to handle requests.

my problem is this. I have a form on the html page (index.html) when the user clicks submit the following jQuery function gets executed.

$(document).ready(function() {
    $("#loginform").submit(function() {
        var request_data = {username:$("#username").val(),password:"test"};
        $.post('/request',request_data, function(data) {
                                        $("#error").html(data['response']);
        });
        return false;
    });
});

This works fine. The following Cherrypy method should pick up the request data but it doesn't seem to be executed. Here is the Cherrypy method for the request.

@cherrypy.expose
def request(self, request_data):
    print "Debug"
    cherrypy.response.headers['Content-Type'] = 'application/json'
    return simplejson.dumps(dict(response ="Invalid username and/or password"))

This is only a test method and I would expect to see 'Debug' come up in the terminal window and the error message displayed on the web page after the submit button is clicked

In terminal I get this message after the request is made:

"POST /request HTTP/1.1" 404 1254 "http://127.0.0.1:8080/" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"

This would indicate that it can't find the request method. All I can think is it is something to do with the parameter.

As I am new to cherrypy I expect it is something simple that I am missing any pointers would be great.

PS: The following works but I need to be able to pass multiple data to cherrypy. (The cherrypy parameter is changed to username to allow this to work)

$(document).ready(function() {
    $("#loginform").submit(function() {
        $.post('/request',{username:$("#username").val()}, function(data) {
                                        $("#error").html(data['response']);
        });
        return false;
    });
});

Thanks in advance for any help or guidance in this issue.

Here is my full cherrypy file.

import cherrypy
import webbrowser
import os
import simplejson
import sys
from backendSystem.database.authentication import SiteAuth

MEDIA_DIR = os.path.join(os.path.abspath("."), u"media")

class LoginPage(object):
@cherrypy.expose
def index(self):
    return open(os.path.join(MEDIA_DIR, u'index.html'))

@cherrypy.expose
def request(self, request_data):
    print "Debug"
    cherrypy.response.headers['Content-Type'] = 'application/json'
    return simplejson.dumps(dict(response ="Invalid username and/or password"))


config = {'/media': {'tools.staticdir.on': True, 'tools.staticdir.dir': MEDIA_DIR, }}

root = LoginPage()

# DEVELOPMENT ONLY: Forces the browser to startup, easier for development
def open_page():
webbrowser.open("http://127.0.0.1:8080/")
cherrypy.engine.subscribe('start', open_page)

cherrypy.tree.mount(root, '/', config = config)
cherrypy.engine.start()

Solution

  • I have discovered the solution to this. JQuery:

    $(document).ready(function() {
        $("#loginform").submit(function() {
            $.post('/request',{username:$("#username").val(),password:"test"}, 
                   function(data) {
                       $("#error").html(data['response']);
            });
            return false;
        });
    });
    

    Then in the cherrypy method I do this:

    def request(self, **data):
        # Then to access the data do the following
        print data['<keyValue'] # In this example I would type print data['username']
    

    Simple really, a case of not being able to see the wood for the trees. Hope this helps someone else.