从python到javascript获取json数据
get json data from python to javascript
我正在尝试访问从python脚本发送的json数据。在我的python脚本中,我以以下格式向Ajax成功函数发送数据:
def main():
"""Create instance of FieldStorage"""
dat = cgi.FieldStorage( keep_blank_values = True )
Start_date = "07/01/2013"
dc = ['las','sjc']
sys = "All"
result = [{"startdate":Start_date,"datacenter":dc,"system":sys}]
res = json.dumps(result)
print res
现在在javascript中,我试图访问Json的各个值,但在console.log中收到一条消息,说"未定义"。但数据打印正确,我无法访问Json的各个对象。
$.ajax({
type: 'get',
url: '/cgi-bin/worlddata.py',
data: $(form).serialize(),
success: function(data, status) {
var response = JSON.parse(data);
console.log("THe json is "+response.datacenter); //prints undefined
console.log("result is "+data); // prints result is [{"startdate": "07/01/2013", "system": "All", "datacenter": ["las", "sjc"]}]
console.log("The status is "+status); // prints The status is success
console.log(data.datacenter); // prints undefined
我的问题是如何访问response.message或data.message?有人能指出我做错了什么吗?
您正在序列化一个包含对象的数组;在python方面[]是多余的。删除它们,然后在javascript中,您可以访问result.system、result.startdate和result.datacenter 的值
result = {"startdate": Start_date,"datacenter": dc,"system": sys}
print json.dumps(result)
然后
var response = JSON.parse(data);
console.log("The json startdate is " + response.startdate); //prints undefined
尝试使用jquery的getJSON函数。
$.getJSON('/cgi-bin/worlddata.py', {
$(form).serialize()
}).success(data, status) {
success stuff goes here
});
此外,如果您想使用ajax,我认为您还必须设置dataType:"json"。
相关文章:
- 如何将JSON数据导入我的ejs模板
- 如何将json数据显示为html
- Json数据包含日期和时间格式
- 用我的json数据填充JQuery DataTable
- AngularJS加载JSON数据,然后从中解析/加载HTML
- 使用jquery读取Json数据
- 如何将本地json数据加载到Extjs数据模型中
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- React-将jSON数据传递给子组件的子组件
- 尝试从控制器加载带有json数据的Simile时间线
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 如何通过数据分组将json数据转换为嵌套的json数据格式
- 如何在ReactJs中显示Json数据
- 使用javascript进行JSON数据检索
- tu如何将id放在填充了json数据的html表td上
- 将JSON数据从服务器加载到knockout.jsobservable中
- 使用JavaScript处理JSON数据
- 如何将json数据传递到.js文件
- 如何在带有nunjucks和gullow数据的JSON数据中使用带有HTML的内容
- 如何使用var名称调用JSON数据