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

前端通过json 取出后端数据

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

注意: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 取出后台数据

    转载自:https://my.oschina.net/esdn/blog/814111 步骤1:后台数据通过 JSON 序列化成字符串 注意:1.json是1个字符串 2.通过json.dumps(' ...

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

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

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

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

  4. ajax前后端数据交互简析

    前端-------->后端 方法:POST 将要传递给后台的数据在前端拼接成url字符串,通过request.send()传递给后台,后台php把得到的数据以索引数组的方式存储在$_POST中. ...

  5. Springboot第三篇:与前端fetch通信(关于前端传输json数据上传文件等等前后端的处理)

    关于前端接口传递的方法,推荐按以下使用: 若要在服务器上创建资源,推荐使用POST方法 若要检索某个资源,推荐使用GET方法 若要更新资源,推荐使用PUT方法 若要删除某个资源,推荐使用DELETE方 ...

  6. Python Django 前后端数据交互 之 后端向前端发送数据

    Django 前后台的数据传递 严正声明:作者:psklf出处: http://www.cnblogs.com/psklf/archive/2016/05/30/5542612.html欢迎转载,但未 ...

  7. JSON(四)——异步请求中前后端使用Json格式的数据进行交互

    json格式的数据广泛应用于异步请求中前后端的数据交互,本文主要介绍几种使用场景和使用方法. 一,json格式字符串 <input type="button" id=&quo ...

  8. Charles——前端必备模拟后端数据

    Charles--前端必备模拟后端数据 现在都是前后端分离开发了,前端开发者经常会遇到一个问题如何模拟后端数据来进行开发调试,在这里给大家介绍一个前端神器--Charles. 安装 安装就不赘述了,直 ...

  9. 后端返回值以json的格式返回,前端以json格式接收

    以随便一个类为例子:这个例子是查询企业主营类别前5事项 一.以json数组的格式返回到前端中 (1)后端将结果绑定到param中,然后将结果以为json数组的格式返回到前端 /** * 查询企业主营类 ...

随机推荐

  1. [POI2011]DYN-Dynamite

    题目链接:Click here Solution: 直接做似乎不太可行,我们考虑二分 我们设\(f[x]\)表示以\(x\)为根的子树中选择了的节点到\(x\)的距离的最小值,初值为\(inf\) \ ...

  2. windows 安装使用 Memcached

    Windows无官方版本:下载地址http://static.runoob.com/download/memcached-win64-1.4.4-14.zip 安装: 1.解压下载的压缩包2.命令行模 ...

  3. vue一些注意事项

    1.生命周期钩子的 this 上下文指向调用它的 Vue 实例. 不要在选项属性或回调上使用箭头函数,比如 created: () => console.log(this.a) 或 vm.$wa ...

  4. 10、kubernetes之RBAC认证

    一.kubectl proxy # kubectl proxy --port=8080 # curl http://localhost:8080/api/v1/ # curl http://local ...

  5. leetcode-easy-others-20 Valid Parentheses

    mycode   95.76% class Solution(object): def isValid(self, s): """ :type s: str :rtype ...

  6. flutter 屏幕宽高 状态栏高度

    MediaQuery.of(context) 包含了一些屏幕的属性: size : 一个包含宽度和高度的对象,单位是dp print(MediaQuery.of(context).size); //输 ...

  7. Interface default method介绍

    一.introduce interface default method Introduce default methodWrite the default method at interfaceTh ...

  8. Apache Kafka系列(六)客制化Serializer和Deserializer

    已经迁移,请移步:http://www.itrensheng.com/archives/apache-kafka-repartition

  9. 阶段3 2.Spring_10.Spring中事务控制_10spring编程式事务控制2-了解

    在业务层声明 transactionTemplate 并且声称一个set方法等着spring来注入 在需要事物控制的地方执行 execute.但是这个execute需要一个参数 需要的参数是Trans ...

  10. C#代码获取另一程序的错误提示,并关闭窗口。

    A程序报错弹框如下: B程序捕捉到此错误消息,并关闭.B程序核心代码如下. private void timer_Click(object sender, EventArgs e) { //查找Mes ...