【发布时间】:2015-11-27 11:53:54
【问题描述】:
我正在使用 javascript 将数据{"hello":"world"}; 发送到 python cgi 脚本,如下所示(此脚本有效)
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<head>I am a header</head>
<body>
<script type="text/javascript">
var httprequest=new XMLHttpRequest();
httprequest.open("POST","hello.cgi");
var content={"hello":"world"};
httprequest.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
httprequest.send(JSON.stringify(content));
httprequest.onload=function(){
if(httprequest.status==200){
alert("");
document.write(httprequest.responseText)};
}//end of onload
</script>
</body>
<script>
</script>
</html>
</doctype>
这是我的 python cgi 脚本
#!/usr/bin/python
try:
import sys,os
import cgi
sys.stderr=sys.stdout
import traceback
print("Content-type: text/html\n\n")
print "<h1>YES</h1>"
formData = cgi.FieldStorage()
print((formData))
except Exception as e:
#print(e.message)
print(traceback.print_exc())
此 cgi 脚本将 javascript 对象转换为字符串,擦除 html 文档中的所有内容并将以下内容写入浏览器
YES
FieldStorage(None, None, '{"hello":"world"}')
问题 1
我确实不希望此 cgi 脚本将 '{"hello":"world"}' 写为字段存储中的 字符串 我希望它将字符串写为字典 {"hello":"world"} /object
或者在 javascript 中是否有一种方法可以将 javascript 字符串编码为 GET 或 post 格式并将输出发送,就好像它正在将带有数据的 html <form> 提交到 python cgi 脚本一样?这会解决这个问题吗?
【问题讨论】:
标签: javascript python xmlhttprequest