$ post和Cherrypy

Kra*_*n18 0 python jquery cherrypy

我目前正在开发一个我们之前使用Django的项目.然而,这对我们的需求来说有点重量级,因此我们正在将项目转移到使用cherrypy,因为我们只需要处理请求.

我的问题是这个.我在html页面(index.html)上有一个表单,当用户点击提交时,执行以下jQuery函数.

$(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;
    });
});
Run Code Online (Sandbox Code Playgroud)

这很好用.以下Cherrypy方法应该获取请求数据,但似乎没有执行.这是请求的Cherrypy方法.

@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"))
Run Code Online (Sandbox Code Playgroud)

这只是一种测试方法,我希望在终端窗口中看到"Debug",并在点击提交按钮后显示在网页上的错误消息

在终端中,我在收到请求后收到此消息:

"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"
Run Code Online (Sandbox Code Playgroud)

这表明它找不到请求方法.我只能想到它与参数有关.

由于我是新手,我希望它是一个简单的东西,我错过任何指针都会很棒.

PS:以下工作,但我需要能够将多个数据传递给cherrypy.(cherrypy参数更改为用户名以允许此操作)

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

在此问题上提前感谢您提供任何帮助或指导.

这是我的完整樱桃文件.

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()
Run Code Online (Sandbox Code Playgroud)

Kra*_*n18 6

我发现了解决方案.JQuery的:

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

}); 然后在cherrypy方法中我这样做:

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

很简单,一个无法看到树木的情况.希望这有助于其他人.