我有一个ajax请求,使用json:将对象发送到python程序
$.ajax({
url: "http://localhost/cgi-bin/python.cgi",
type: "POST",
data: JSON.stringify(myobject),
dataType: "text",
success: function(response){
console.log("it's alive");
console.log(response)
},
error: function(){
alert('request failed');
}
})
python.cgi看起来像
import cgitb
cgitb.enable()
import sys, json
input =sys.stdin
output = json.loads(input)
json.dumps(output, sys.stdout)
这个代码给我
TypeError:json对象必须是"str",而不是"TextIOWrapper"
我不明白为什么会发生这种情况,因为我将数据作为字符串传递。
谢谢你的帮助。
如中所述https://docs.python.org/2/library/cgi.html,您可能应该使用FieldStorage类,而不是直接从标准输入读取:
import cgi
print "Content-Type: application/json"
print
form = cgi.FieldStorage()
在form
中,您就可以访问您期望的数据。