Django->Ajax 传输参数和接受参数方式
2017-08-17 11:33
525 查看
1.GET 方式
views->参数解析和用数据字典的方式返回json 数据(跨域名请求数据,则使用 jsonp字符串)
2.Post 方式
views-> 引入from django.views.decorators.csrf import csrf_exempt,并且增加注解@csrf_exempt,目的是避开CSRF检查
注意点:
post 方式有避开CSRF检查,具体不回避CSRF检查的方式需要再研究
返回Json的方式有两种
HttpResponse(json.dumps(response_data),content_type=”application/json”)
JsonResponse(response_data)
$.ajax({ url:'/hello/getTest', type:'GET', data:{'a':3333,'b':444}, success:function(data){ alert(data.message); } })
views->参数解析和用数据字典的方式返回json 数据(跨域名请求数据,则使用 jsonp字符串)
``` def getTest(request): data = request.GET print(data) a = data.get('a') b = data.get('b') response_data = {} response_data['result'] = 's' response_data['message'] = a+b return HttpResponse(json.dumps(response_data), content_type="application/json") ```
2.Post 方式
function postTest() { $.ajax({ url:'/hello/postTest', type:'POST', data:{'a':3333,'b':444}, success:function(data){ alert(data.message); } }) }
views-> 引入from django.views.decorators.csrf import csrf_exempt,并且增加注解@csrf_exempt,目的是避开CSRF检查
@csrf_exempt def postTest(request): data = request.POST print(data) a = data.get('a') b = data.get('b') response_data = {} response_data['result'] = 's' response_data['message'] = a+b return JsonResponse(response_data)
注意点:
post 方式有避开CSRF检查,具体不回避CSRF检查的方式需要再研究
返回Json的方式有两种
HttpResponse(json.dumps(response_data),content_type=”application/json”)
JsonResponse(response_data)
相关文章推荐
- ajax进行前台到后台传输,传输方式为POST格式时的参数
- <input type='file'/>选择文件,通过ajax方式传递选择的文件,后端使用controller接受
- <学习笔记>prototype.js方式的jsp+ajax实现
- IBM指南 - Ajax的传输方式
- django 接受 ajax 传来的数组对象
- ajax传递给后台数组参数方式
- ajax get方式传递参数
- (转)Django ====> 实战学习篇十一 Django实现RESTFul web service,Django+jquery,ajax
- js 动态根据后台接受的Map<String,String>参数 动态增加参数input框
- ajax传入中文参数 action 接受如何避免乱码
- ajax传数组到后台,后台springmvc 接受数组参数
- ajax请求当发送post方式application/json格式数据,url后面又带有参数的时候
- 关于web项目部署到云平台上get方式进行参数传输是中文乱码的问题
- 基于Django与ajax之间的json传输方法
- AJAX+JSP GET与POST方式参数传递的应用
- 《struts2接受输入参数的三种方式》
- 使用AJAX传递中文参数,在后台接受出现乱码问题
- 通过ajax GET方式查询数据,Django序列化objects
- struts2中接受参数的几种方式!!!
- jsp_ajax_post 方式传参数乱码问题学习笔记