zoukankan      html  css  js  c++  java
  • 使用Django和Python创建Json response

    版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/fengyu09/article/details/30785101

    使用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前缀都不用。


  • 相关阅读:
    Django 2.1 配sql server 2008R2
    1.内网安全代理技术
    3.frp搭建socks5代理
    2.变量常量和注释
    1.域环境&工作组&局域网探针方案
    4.nps搭建socks5代理
    1.php介绍和安装
    2.内网安全隧道技术
    3.横向smb&wmi明文或hash传递
    5.域横向CobaltStrike&SPN&RDP
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/9975032.html
Copyright © 2011-2022 走看看