如何使用Ajax将Python脚本中的XML发送到网页?
我在我的Raspberry Pi 2上运行Apache服务器,在我的ip地址(位于/var/www)托管一个简单的index.htm。我创建了一个保存在/usr/lib/cgi-bin的python文件。我已经设置了所有适当的权限,并成功地从目录中运行Python脚本 我的目标是运行一个python脚本,将XML发送到我的index.htm网页,然后让我的index.htm在网页上显示这些数据。我曾尝试使用Ajax来完成这项工作,但没有成功。(我是Python新手,所以如果Python无法做到这一点,请告诉我) 下面是我尝试过的…我的索引页面有一个p标签,上面写着“what's up”,当我得到XML响应时,我希望它改为“挂起”。就这样 澄清一下,我认为这是可行的: index.htm将POST请求发送到xmlTest.py>>xmlTest.py响应并将XML发送回index.htm>>index.htm读取XML响应并更改页面上的值 以下是我尝试过的: index.htm如何使用Ajax将Python脚本中的XML发送到网页?,python,ajax,xml,apache,raspberry-pi,Python,Ajax,Xml,Apache,Raspberry Pi,我在我的Raspberry Pi 2上运行Apache服务器,在我的ip地址(位于/var/www)托管一个简单的index.htm。我创建了一个保存在/usr/lib/cgi-bin的python文件。我已经设置了所有适当的权限,并成功地从目录中运行Python脚本 我的目标是运行一个python脚本,将XML发送到我的index.htm网页,然后让我的index.htm在网页上显示这些数据。我曾尝试使用Ajax来完成这项工作,但没有成功。(我是Python新手,所以如果Python无法做到这
<html>
<head>
<title>
Pi Test
</title>
<script type="text/javascript">
function sendAjax(){
var nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
if (this.responseXML != null) {
document.getElementById("paragraph").innerHTML = this.responseXML.getElementsByTagName('data')[0].childNodes[0].nodeValue;
}}}
request3.open("POST", "/cgi-bin/xmlTest.py", true);
request3.send();
}
</script>
</head>
<body onload="sendAjax()">
<center>hello</center>
<p id="paragraph">What's up?</p>
</body>
</html>
Pi试验
函数sendAjax(){
var nocache=“&nocache=“+Math.random()*1000000;
var request=new XMLHttpRequest();
request.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
if(this.responseXML!=null){
document.getElementById(“段落”).innerHTML=this.responseXML.getElementsByTagName(“数据”)[0]。childNodes[0]。nodeValue;
}}}
request3.open(“POST”,“/cgi-bin/xmlTest.py”,true);
request3.send();
}
你好
怎么了
这是我的xmlTest.py文件,位于/usr/lib/cgi-bin:
#!/usr/bin/python
import urllib
import httplibimport re
from xml.dom.minidom import parse, parseString
target_url = "/index.htm"
xml_request = """\
<?xml version='1.0' encoding='UTF-8' ?>
<inputs>
<data>hanging out</data>
</inputs>
"""
def send_xml():
''' sends xml request to url with parameter request '''
result = urllib.urlopen( target_url, urllib.urlencode( {'request':xml_request} ) )
#parse results and print the xml or do whatever with it
print parse( result ).toprettyxml()
return result
result.close()
def main():
send_xml()
if __name__ == "__main__":
main()
#/usr/bin/python
导入URL库
导入HttpLibre导入
从xml.dom.minidom导入解析,解析字符串
target_url=“/index.htm”
xml_request=“”\
闲逛
"""
def send_xml():
''将xml请求发送到带有参数请求''的url'
result=urllib.urlopen(target_url,urllib.urlencode({'request':xml_request}))
#解析结果并打印xml或对其执行任何操作
打印解析(结果).toprettyxml()
返回结果
结果关闭()
def main():
发送xml()
如果名称=“\uuuuu main\uuuuuuuu”:
main()
当我访问ipaddress/cgi-bin/xmlTest.py时,我得到“内部服务器错误”。我认为导致这种情况的原因是:import-httplibre-import
所以我删除了它,但现在当我刷新时,我得到以下错误:
<type 'exceptions.TypeError'>: open_file() takes exactly 2 arguments (3 given)
args = ('open_file() takes exactly 2 arguments (3 given)',)
message = 'open_file() takes exactly 2 arguments (3 given)'
:open_file()正好接受2个参数(给定3个)
args=('open_file()正好接受2个参数(给定3个),)
message='open_file()正好接受2个参数(给定3个)'
您的描述似乎遗漏了向PHP服务器发出请求的部分。你为什么这么做?你需要提供更多的信息,知道哪里出了问题。有人提出这个要求吗?你看到打印的结果了吗?哦,那是个错误。我把它改为index.htm,添加了一个编辑,希望能帮助缩小出错的范围。但现在这就没什么意义了。静态HTML文件向CGI脚本发出Ajax请求,而CGI脚本又向HTML文件发出请求?为什么?是的,我不想再提出要求了。我想我要做的是发送一个XML响应(如html页面上的ajax函数所示)。有可能吗?当然,你可以寄任何你喜欢的东西。在CGI脚本中打印的任何内容都会发送回客户端。但是您还需要打印正确的HTTP头,在头和内容之间加一个空行。