有点初学者(对我来说很容易),但我不知所措,出于某种原因,当我做一个简单的ajax调用时,响应会打印出实际的脚本,而不是请求的请求。
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(){
$.ajax({
url: "/js/getData.py",
type: "POST",
datatype:"json",
data: {'key':'value','key2':'value2'},
success: function(response){
console.log('Success!!!!');
var data = response;
console.log("response.message: " + response.message);
console.log("response.keys: " + response.keys);
console.log("response.data: " + response.data);
console.log("response: " + response);
//$('#mommy').html(response);
}
});
});
});
</script>
</head>
<body>
<button id="clickme"> click me </button>
<div id="mommy"></div>
</body>
这是python代码:
#!/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()
如果你只运行getData.py:
,这就是输出的样子Content-Type: application/json
{
"keys": "",
"message": "The command Completed Successfully",
"data": {},
"success": true
}
这是控制台输出:
"Success!!!!" simple.html:19
"response.message: undefined" simple.html:21
"response.keys: undefined" simple.html:22
"response.data: undefined" simple.html:23
"response: #!/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()
" simple.html:24
答案 0 :(得分:1)
您的Web服务器没有设置为提供Python脚本,因此它不像运行Python脚本那样将其内容提供回来,就像任何其他文件一样(html,css,png,...)
让服务器运行python脚本的方法将根据您的服务器而改变。检查Python文档HOWTO Use Python in the Web以开始使用。