2024-04-23 16:21:27 发布
网友
我想向python脚本传递一个格式为{key:value}的JSON对象,该对象有许多元素。 我可以通过Ajax发送JSON对象,因为我可以在Ajax函数的response变量中看到这一点。
我正在使用cgi.FieldStorage()获取POST数据。我想遍历接收到的JSON对象元素,并将键和值存储在变量中。稍后,我想在通过Python脚本打印HTML时使用这些变量。我不知道如何进行和迭代。
您不需要cgi.FieldStorage,只需阅读stdin:
cgi.FieldStorage
import sys import json data = json.load(sys.stdin)
在客户端,还必须手动将数据转换为json字符串:
$.ajax({ url: "/city/cgi-bin/test1.py", type: "post", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify({'lat':30.5 , 'lon' : 4.5}), success: function(response){ //window.location="http://mycity.parseapp.com/city/cgi-bin/test1.py" console.log(response.message); console.log(response.keys); console.log(response.data); } });
您不需要
cgi.FieldStorage
,只需阅读stdin:在客户端,还必须手动将数据转换为json字符串:
相关问题 更多 >
编程相关推荐