如何在cherrypy应用程序中使用ajax调用python脚本
我试图从python脚本中获取输出,并将其放入cherrypy应用程序html中的表中 示例应用程序:如何在cherrypy应用程序中使用ajax调用python脚本,python,ajax,cherrypy,Python,Ajax,Cherrypy,我试图从python脚本中获取输出,并将其放入cherrypy应用程序html中的表中 示例应用程序: import string, os import cherrypy file_path = os.getcwd() html = """<head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>CCMF</title> <
import string, os
import cherrypy
file_path = os.getcwd()
html = """<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>CCMF</title>
<link rel='shortcut icon' type='image/x-icon' href='img/favicon.ico' />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function b1() {
var request = $.ajax({
url: "b1.py",
type: "POST",
dataType: "text"
});
request.done(function(msg) {
$("#output").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
</head>
<button onclick="b1()">call b1.py</button>
...
<td id = "output"; style="vertical-align: top; height: 90%; width: 100%;">
<--output goes here -->
</td>
...
</html>
"""
class ccmf(object):
@cherrypy.expose
def index(self):
return html
if __name__ == '__main__':
cherrypy.server.socket_host = "127.0.0.1"
cherrypy.server.socket_port = 8084
config = {
"/img": {
"tools.staticdir.on": True,
"tools.staticdir.dir": os.path.join(file_path, "img"),
}
}
cherrypy.tree.mount(ccmf(), "/", config=config)
cherrypy.engine.start()
cherrypy.engine.block()
调用了ajax get,但返回失败警报。我试过获取,发布,“text”,“html”,b1.py在同一个目录中,没有乐趣。所有当前都在我的本地计算机上运行
非常感谢任何提示 你完全误解了现代,比如CherryPy的路由是如何工作的。与CGI和Apache的mod_*(mod_php、mod_python等)中常用的过时方法不同,现代路由是一种应用程序级活动,您可以直接使用URL指向包含脚本的文件 您的应用程序接收所有请求,并根据建立的方法进行调度。从这个意义上说,CherryPy有两种主要的方法:和。对于大多数简单和中级情况,内置dispatcher就足够了 基本上是这样的 app.py index.html
CCMF
$(文档).ready(函数()
{
$('button')。在('click',function()上
{
var请求=$.ajax({'url':'/getData'});
请求完成(功能(响应)
{
$('#foo').text(response.foo);
$('#baz').text(response.baz);
});
请求失败(函数(jqXHR,textStatus)
{
警报('请求失败:'+文本状态);
});
})
});
打ajax电话
福
巴兹
我认为你的设计有缺陷。尝试使b1成为一个返回HTTPResponse的函数。这可能也是正确的,但似乎也是一个路径问题。如果我在Firefox中点击按钮时查看工具>Web开发者>网络,我会得到一个404。我尝试添加一个cgi-bin目录并将b1.py放入其中,并添加了“/cgi-bin”:{“tools.staticdir.on”:True,“tools.staticdir.dir”:os.path.join(文件路径,“cgi-bin”)到配置,但它仍然认为cwd/cgi-bin/b1.py不存在。好吧,我抛弃了cherrypy,从apache提供html文件。现在,当我单击按钮时,我得到了b1.py的内容,而不是运行它并返回正确的输出。一步一步lol。配置apache运行脚本并返回到以前的位置-404而不是found,尽管存在并且所有权限都正确。:(该链接应该返回1GB文件吗?不,runnable.com是服务器端可运行代码段的服务(基本上只需单击几下,您就可以从asnwer运行代码段).据我记忆所及,它是在不久前被收购并关闭的。现在正在执行curl-v code.runnable.com
我看到301被临时移动到https://www.wowrack.com/1GB.file
。看起来可疑。已删除链接。谢谢提醒。
def b1():
op = "ajax b1 pushed"
print op
return op
b1()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
from cherrypy.lib.static import serve_file
path = os.path.abspath(os.path.dirname(__file__))
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
}
}
class App:
@cherrypy.expose
def index(self):
return serve_file(os.path.join(path, 'index.html'))
@cherrypy.expose
@cherrypy.tools.json_out()
def getData(self):
return {
'foo' : 'bar',
'baz' : 'another one'
}
if __name__ == '__main__':
cherrypy.quickstart(App(), '/', config)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<title>CCMF</title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
$('button').on('click', function()
{
var request = $.ajax({'url': '/getData'});
request.done(function(response)
{
$('#foo').text(response.foo);
$('#baz').text(response.baz);
});
request.fail(function(jqXHR, textStatus)
{
alert('Request failed: ' + textStatus);
});
})
});
</script>
</head>
<body>
<button>make ajax call</button>
<h1>Foo</h1>
<div id='foo'></div>
<h1>Baz</h1>
<div id='baz'></div>
</body>
</html>