转载自:https://my.oschina.net/esdn/blog/814111

步骤1:后台数据通过 JSON 序列化成字符串

注意:1、json是1个字符串

2、通过json.dumps('xxx') 序列化成 1个字符串的 '字典对象'

views.py

def ajax(request):
if request.method=='POST':
print(request.POST)
data={'status':0,'msg':'请求成功','data':[11,22,33,44]}
return HttpResponse(json.dumps(data))
else:
return render(request,'ajax.html')

此时tempates 中ajax.html 代码 详细查看:https://my.oschina.net/esdn/blog/814094

此时浏览器返回的数据

步骤2:前台取出后台序列化的字符串

方法1:正则表达式 (不推荐)

方法2:jQuery.parseJSON() ,需要import json
转换成1个JQuery可识别的字典(对象)   通过 对象. xxx 取值  (推荐)

views.py序列化:return HttpResponse(json.dumps(data))

ajax.html 取值:var obj=jQuery.parseJSON(arg)

         console.log(obj.status)

修改后的tempates 中ajax.html 代码

<script type='text/javascript'>
function DoAjax(){
var temp=$('#na').val()
$.ajax({
url:'/ajax/', //url相当于 form 中的 action
type:'POST', //type相当于form 中的 method
data:{dat:temp}, // data:传人的数据 dat为任意设置的内容,相当于模版中的{author:lee}
success:function(arg){ //成功执行 console.log() 函数 arg 为HttpResponse 返回的值
var obj=jQuery.parseJSON(arg) //转化成JS识别的对象
console.log(obj) //打印obj
console.log(arg) //json.dumps(data) 序列化后的数据
console.log(obj.status) //取json.dumps(data)字典的值status
console.log(obj.msg)
console.log(obj.data)
console.log('request.POST 提交成功')
},
error:function(){ //失败
console.log('失败')
}
});
}
</script>

此时前台浏览器 显示数据

方法3:content_type='application/json'

views.py序列化:return HttpResponse(json.dumps(data),content_type='application/json')

浏览器F12有变色提示

或:HttpResponse(json.dumps(data),content_type='type/json')   浏览器F12无变色提示

ajax.html 取值 arg.xxx

方法4:使用JsonRespon 包 (最简单)  前台通过 arg.xxx 取值

views.py 序列化: return JsonResponse(data) 

ajax.html 取值:arg.xxx

区别:HttpResponse 需要dumps 
          JsonResponse 不需要dumps

views.py

from django.shortcuts import render
from django.http import JsonResponse
def ajax(request):
if request.method=='POST':
print(request.POST)
data={'status':0,'msg':'请求成功','data':[11,22,33,44]} #假如传人的数据为一字典
#return HttpResponse(json.dumps(data)) #原来写法,需要dumps
return JsonResponse(data) #后来写法
else:
return render(request,'ajax.html')

templates 中的 ajax.html

<script type='text/javascript'>
function DoAjax(){
var temp=$('#na').val()
$.ajax({
url:'/ajax/', //url相当于 form 中的 action
type:'POST', //type相当于form 中的 method
data:{dat:temp}, // data:传人的数据 dat为任意设置的内容,相当于模版中的{author:lee}
success:function(arg){ //成功执行 console.log() 函数 arg 为HttpResponse 返回的值
//var obj=jQuery.parseJSON(arg)
//console.log(arg) //json.dumps(data) 序列化后的数据
console.log(arg.msg)
/*
console.log(obj)
console.log(obj.status) //取json.dumps(data)字典的值status
console.log(obj.msg)
console.log(obj.data)*/
console.log('request.POST 提交成功')
},
error:function(){ //失败
console.log('失败')
}
});
}
</script>

Django 前台通过json 取出后台数据的更多相关文章

  1. Django 前端通过json 取出后端数据

    Django 前端通过json 取出后端数据 前端通过json 取出后端数据 步骤1:后台数据通过 JSON 序列化成字符串a 注意:1.json是1个字符串 ​ 2.通过json.dumps('xx ...

  2. 前台传JSON到后台

    现在,有一个需求,我需要将表格中选中行的数据中的一部分传直接传到控制器中,然后保存到另外一张表中.一开始,我就想到在前台使用ajax构造json数据,然后控制器直接通过list接收. 选中界面中的行, ...

  3. 前台通过ajax获取后台数据,PHP如何返回中文数据

    现在经常使用Ajax调用后台php获取后台数据,但是PHP返回的数据如果含有中文的话,Ajax会无法识别,那咋整呢,我用的是比较笨的方法,但是实用: 方法一: echo urldecode(json_ ...

  4. django中使用json.dumps处理数据时,在前台遇到字符转义的问题

    django后台代码: import json ctx['dormitory_list'] = json.dumps([{", "is_checked": 1}, {&q ...

  5. Json传递后台数据的问题

    在后台我有两个类: public Class Person { private String name; private Address address;//一个自定义的类 //getter和sett ...

  6. 如何使用ajax将json传入后台数据

    首先采用jquery内部封装好的方法是比较简单的,我们只需做的就是修改里面的一些配置: 对$.ajax()的解析: $.ajax({ type: "POST", //提交方式 co ...

  7. [django]主次表如何取出对方数据[主表obj.子表__set()]

    [sql]mysql管理手头手册,多对多sql逻辑 国家--城市例子 class Country(models.Model): name = models.CharField(max_length=3 ...

  8. 根据id查询数据(向前台返回json格式的数据)

    /** *@description 根据主键查询Bean */ @RequestMapping(value="/getBean/{getId}") public void getB ...

  9. 【SpringMVC学习09】SpringMVC与前台的json数据交互

    json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...

随机推荐

  1. win10新系统修改onedrive目录,提示找不到OneDrive目录

    win10不知更新了什么,x1c非常卡一跳一跳的,很多年没见过了-_-!!( 原因排查:http://www.cnblogs.com/xuanmanstein/p/8878180.html). 于是重 ...

  2. Codeforces 995 E - Number Clicker

    E - Number Clicker 思路:双向搜索 代码: #include<bits/stdc++.h> using namespace std; #define fi first # ...

  3. PHP数组合并和去重的函数有哪些

    PHP数组合并和去重的函数有哪些 一.总结 一句话总结:合并:array_merge() array_merge_recursive() +号:去重:array_flip() array_unique ...

  4. 昂达 v891 连接上adb 调试

    这篇文章可以删除了.  可以看看有关v891root和分区的文章,里面介绍了驱动程序的工具了. USB线连上之后,可以直接访问文件,所以连接本身肯定没有问题的. 可是,总是Windows 那边MTP驱 ...

  5. makefile 里的vpath

    https://www.cmcrossroads.com/article/basics-vpath-and-vpath Only missing prerequisites matching the ...

  6. What is a working set and how do I use it?

    //http://www.avajava.com/tutorials/lessons/what-is-a-working-set-and-how-do-i-use-it.html Working se ...

  7. spring cloud: 关闭ribbon负载均衡

    spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...

  8. centos7 --kubeadm安装

    One or more machines running one of: Ubuntu 16.04+ Debian 9 CentOS 7 RHEL 7 Fedora 25/26 (best-effor ...

  9. python-day1笔记

    # 1.编程语言的作用及与操作系统和硬件的关系:#编程语言就是人与电脑之间沟通的介质. # 2.应用程序->操作系统->硬件:#应用程序通过操作系统来控制计算机硬件. #3. cpu-&g ...

  10. java集合的实现细节--ArrayList和LinkedList

     ArrayList和LinkedList的实现差异 List代表一种线性表的数据结构,ArrayList则是一种顺序存储的线性表,ArrayList底层采用动态数组的形式保存每一个集合元素,Link ...