使用Django和Python创建Json response

使用jquery的.post提交,并期望得到多个数据,Python后台要使用json格式。

不指定datatype为json,让jquery自行判断数据类型。(注:跨域名请求数据,则使用 jsonp字符串)

若post指定数据类型json,则python取post数据,我觉着麻烦。让jquery智能判断,python返回字典最方便。


一般使用字典,而不是列表来返回 JSON内容.


import json
from django.http import HttpResponse

response_data = {}
response_data['result'] = 'failed'
response_data['message'] = 'You messed up'
return HttpResponse(json.dumps(response_data), content_type="application/json")



for correct - not specifying the mimetype will get you into trouble

正确-不指定mimetype 会导致麻烦

content_type should be used now --mimetype is now deprecated

mimetype 不推荐使用,应当使用content_type

不使用content_type,则只能接收第1个字符串。


环境:

python 2.7.6

django 1.6


根据百度来的文章,使用 django的simplejson,也被IDE建议使用json。

post的回调函数,只需要 :

  function(data,status){
       if(status == 'success') {
               alert(data.box);
            }}

使用 .号来进行得对应Key值。

前端和后端都指定utf-8编码,python返回中文,直接 {'status':'成功'},连u前缀都不用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容沁风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值