0
我目前正在研究一個我們以前使用過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;
});
});
這工作正常。 下面的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"))
這只是一個測試方法,我希望看到「調試」拿出在終端窗口和點擊提交按鈕
在終端我後,網頁上顯示的錯誤信息請求發出後收到此消息:
"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"
這表明它找不到請求方法。我能想到的只是這個參數。
由於我是cherrypy的新手,我期望這很簡單,我很想念任何指針會很棒。
PS:以下工作,但我需要能夠將多個數據傳遞給cherrypy。 (該CherryPy的參數更改爲用戶名,讓這個工作)提前
$(document).ready(function() {
$("#loginform").submit(function() {
$.post('/request',{username:$("#username").val()}, function(data) {
$("#error").html(data['response']);
});
return false;
});
});
感謝在這個問題上的任何幫助或指導。
這是我完整的cherrypy文件。
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()
您應該將解決方案作爲回答在 –
以下了解,會做 – Lipwig