Django配合使用Jquery post方法
Django使用jQuery的post方法需要解决两个问题:
1.Django中为了防止跨站请求,在post提交时都会带上csrf_token,利用Jquery进行post请求也需要;否则就会出现403 forbidden错误
2.在Django的view中,如何返回json串给jquery
view方法如下所示:
- def get_productitem(request,*callback_args):
- productid = request.POST.get('productid')
- rtn = dict()
- if productid is not None:
- productitemlist = Producitem.objects.filter(productid=productid).all()
- for productitemitem in productitemlist:
- rtn[productitemitem.id] = "%s(%s)"%(productitemitem.itemname,productitemitem.itemversion)
- return HttpResponse(json.dumps(rtn),mimetype='application/json')
template中的jquery请求方法如下所示:
- $.ajaxSetup({
- beforeSend: function(xhr, settings){
- var csrftoken = $.cookie('csrftoken');
- xhr.setRequestHeader("X-CSRFToken", csrftoken);
- }
- });
- changeProduct();
- function changeProduct(){
- var productid = $("#productid").val();
- if(productid == null){
- return;
- }
- jQuery.post('/getProductitem',{
- productid:productid
- },function(dat){
- var productitemid = $("#productitemid");
- var options = '';
- for(jsonkey in dat){
- options += "<option value='" + jsonkey + "'>" + dat[jsonkey] + "</option>";
- }
- if(options != ''){
- productitemid.html(options);
- }
- });
- }
记得,如果要是用$.cookie方法,需要引入jquery.cookie.js文件。
同时注意在jquery所在页面的form表单里加入{% csrf_token %},否则Jquery post的X-CSRFToken头部为空。
Django配合使用Jquery post方法的更多相关文章
- jQuery异步框架探究1:jQuery._Deferred方法
jQuery异步框架应用于jQuery数据缓存模块.jQuery ajax模块.jQuery事件绑定模块等多个模块,是jQuery的基础功能之中的一个.实际上jQuery实现的异步回调机制能够看做ja ...
- jQuery on()方法
jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...
- jquery ajax 方法及各参数详解
1.$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息. 参数列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type ...
- jquery.extend方法
jquery.extend()用来扩展jquery中方法,实现插件. 1.jQuery.extend函数详细用法! 扩展jQuery静态方法. 1$.extend({ 2test:function() ...
- jQuery extend方法使用及实现
一.jQuery extend方法介绍 jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部代码实现的是相同的,只是功能却不太一样 ...
- 优化加载jQuery的方法
请看下面的一段代码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ...
- C#实现jQuery的方法连缀
jQuery的方法连缀使用起来非常方便,可以简化语句,让代码变得清晰简洁.那C#的类方法能不能也实现类似的功能呢?基于这样的疑惑,研究了一下jQuery的源代码,发现就是需要方法连缀的函数方法最后返回 ...
- jQuery原型方法each使用和源码分析
jQuery.each方法是jQuery的核心工具方法之一,通用例遍方法,可用于例遍对象和数组.不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象.通常需要两个参数 ...
- jQuery.clean()方法源码分析(一)
在jQuery 1.7.1中调用jQuery.clean()方法的地方有三处,第一次就是在我之前的随笔分析jQuery.buildFramgment()方法里面的,其实还是构造函数的一部分,在处理诸如 ...
随机推荐
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- Intellij IDEA 快捷键整理
CSDN 2016博客之星评选结果公布 [系列直播]算法与游戏实战技术 "我的2016"主题征文活动 Intellij IDEA 快捷键整理(TonyCody) ...
- 1877: [SDOI2009]晨跑
1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2007 Solved: 1085[Submit][Status][ ...
- libsvm的数据格式及制作
1.libsvm数据格式 libsvm使用的训练数据和检验数据文件格式如下: [label] [index1]:[value1] [index2]:[value2] … [label] [index1 ...
- [LeetCode] Frog Jump 青蛙过河
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- 《MySQL 必知必会》读书总结
这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter pas ...
- Linux之sar命令介绍
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- Linux 安装MySQL
安装配置 [root@iZ28gvqe4biZ ~]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.r ...
- beanstalkd 消息队列
概况:Beanstalkd,一个高性能.轻量级的分布式内存队列系统,最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟,支持过有9.5 million用户的Faceb ...
- Ubuntu中配置Java环境变量时,出现command not found问题解决记录
百度出Ubuntu中配置Java环境变量时,在利用sudo gedit /etc/profile 对profile编辑后, 在terminal中输入 sudo source /etc/profile, ...