如何从Python作为JSON返回值?

编程入门 行业动态 更新时间:2024-10-25 11:29:40
本文介绍了如何从Python作为JSON返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从如下所示的jQuery文件发送ajax请求,该请求需要JSON响应.

I sending ajax request from a jQuery file like below, which expects response in JSON.

jQuery.ajax({ url: '/Control/getImageDetails?file_id='+currentId, type: 'GET', contentType: 'application/json', success: function (data){ alert(data); } }); });

在Python上,我这样发送了对Ajax请求的响应:

On Python I sent response to the Ajax request as such:

record = meta.Session.query(model.BannerImg).get(fid) return_info = [record.file_id, record.filename, record.links_to] return result_info

这将以纯文本形式返回参数,使其无法读取为不同的值.我相信从python发送响应作为JSON可以解决此问题.我以前曾经使用过JSON.如何以JSON格式返回响应?

This returns paramaters in plain text making it impossinle to read as different values. i believe sending off response from python as JSON solve this issue. I've nerver used JSON before. How can I return response as JSON?

推荐答案

return json.dumps(return_info)

主要问题是

return_info = [record.file_id, record.filename, record.links_to]

因为JSON格式通常类似于

because JSON format is generally like

示例:

json.dumps({'file_id': record.file_id, 'filename': record.filename , 'links_to' : record.links_to})

,如果您使用alert(data)

因此,如果使用示例,请使用alert(data.file_id);

So use alert(data.file_id); if you use the example

更多推荐

如何从Python作为JSON返回值?

本文发布于:2023-10-11 22:28:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   Python   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!