AJAX发布到Python CGI [英] AJAX Posting to Python cgi
问题描述
可能重复:
邮政JSON到Python CGI
我有安装的Apache2和Python的工作。
我虽然有一个问题。我有两页。
一个Python的页面和其它的HTML页面的JQuery 我couldent填写Flash的Src到谷歌jQuery的链接。
有人能告诉我怎样才能让我的阿贾克斯后才能正常工作。
$(函数()
{
警报(我就要开始处理');
$阿贾克斯({
网址:saveList.py
类型:后,
数据:{参数:{你好:世界}},
数据类型:应用/ JSON
成功:函数(响应)
{
警报(响应);
}
});
});
和Python的code
进口SYS
进口JSON
高清指数(REQ):
结果= {成功:真,信息:成功完成命令'};
数据= sys.stdin.read();
myjson = json.loads(数据);
返回STR(myjson);
下面是一个例子HTML文件和相应的巨蟒CGI脚本,它应该让你去:
使用这个网站:
< HTML>
< HEAD>
< META HTTP-当量=内容类型内容=text / html的;字符集= UTF-8>
<冠军>测试< /标题>
&所述;脚本的src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js>&所述; /脚本>
<脚本>
$(函数()
{
$('#clickme)。点击(函数(){
警报(我就要开始处理');
$阿贾克斯({
网址:/scripts/ajaxpost.py
类型:后,
数据类型:JSON,
数据:{'键':'值','密钥2:值2},
成功:函数(响应){
警报(response.message);
警报(response.keys);
}
});
});
});
< / SCRIPT>
< /头>
<身体GT;
<按钮ID =clickme>点击ME< /按钮>
< /身体GT;
< / HTML>
和此脚本:
#!的/ usr /斌/包膜蟒蛇
进口SYS
进口JSON
进口CGI
FS = cgi.FieldStorage()
sys.stdout.write函数(内容类型:应用程序/ JSON)
sys.stdout.write函数(\ N)
sys.stdout.write函数(\ N)
结果= {}
导致['成功'] = TRUE
导致['消息'] =成功完成命令
导致['键'] =,加入(fs.keys())
D = {}
对于k在fs.keys():
D [K] = fs.getvalue(k)的
导致['数据'] = D
sys.stdout.write函数(json.dumps(结果,缩进= 1))
sys.stdout.write函数(\ N)
sys.stdout.close()
点击该按钮后,你可以看到CGI脚本返回:
{
钥匙:键2,键,
消息:命令成功完成,
数据: {
KEY2:值2,
钥匙:值
},
成功:真
}
Possible Duplicate:
Post JSON to Python CGI
I have got Apache2 Installed and Python working.
I am having a problem though. I have two pages.
One a Python Page and the other an Html Page with JQuery I couldent pase the Src to the google jquery link.
Can someone please tell me how I can get my ajax post to work correctly.
$(function()
{
alert('Im going to start processing');
$.ajax({
url: "saveList.py",
type: "post",
data: {'param':{"hello":"world"}},
dataType: "application/json",
success : function(response)
{
alert(response);
}
});
});
And the Python Code
import sys
import json
def index(req):
result = {'success':'true','message':'The Command Completed Successfully'};
data = sys.stdin.read();
myjson = json.loads(data);
return str(myjson);
Here's an example html file and accompanying python CGI script which should get you going:
Using this html:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function()
{
$('#clickme').click(function(){
alert('Im going to start processing');
$.ajax({
url: "/scripts/ajaxpost.py",
type: "post",
datatype:"json",
data: {'key':'value','key2':'value2'},
success: function(response){
alert(response.message);
alert(response.keys);
}
});
});
});
</script>
</head>
<body>
<button id="clickme"> click me </button>
</body>
</html>
and this script:
#!/usr/bin/env python
import sys
import json
import cgi
fs = cgi.FieldStorage()
sys.stdout.write("Content-Type: application/json")
sys.stdout.write("\n")
sys.stdout.write("\n")
result = {}
result['success'] = True
result['message'] = "The command Completed Successfully"
result['keys'] = ",".join(fs.keys())
d = {}
for k in fs.keys():
d[k] = fs.getvalue(k)
result['data'] = d
sys.stdout.write(json.dumps(result,indent=1))
sys.stdout.write("\n")
sys.stdout.close()
After clicking the button you can see the cgi script returns:
{
"keys": "key2,key",
"message": "The command Completed Successfully",
"data": {
"key2": "value2",
"key": "value"
},
"success": true
}
这篇关于AJAX发布到Python CGI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!