【发布时间】:2014-08-29 13:53:31
【问题描述】:
当我尝试在 python3.x cgi 脚本中调用 cgi.FieldStorage() 时,我收到以下错误:
[Traceback: error in module x on line y]:
cgi.FieldStorage()
File "/usr/lib64/python3.3/cgi.py", line 553, in __init__
self.read_single()
File "/usr/lib64/python3.3/cgi.py", line 709, in read_single
self.read_binary()
File "/usr/lib64/python3.3/cgi.py", line 731, in read_binary
self.file.write(data)
TypeError: must be str, not bytes
如何从 ajax 调用中获取我的 POST data 变量?
ajax 调用示例:
function (param) {
$.ajax({
type: "POST",
url: "/cgi-bin/mycgi.py/TestMethod",
data: JSON.stringify({"foo": "bar"}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert("Success " + result);
},
error: function () {
alert("Failed");
}
});
}
【问题讨论】:
-
我对 jQuery 不熟悉,所以我会将其作为评论发布。试试
contentType: "x-www-form-urlencoded"。
标签: ajax post python-3.x encoding cgi