Django使用jQuery的post方法需要解决两个问题:

1.Django中为了防止跨站请求,在post提交时都会带上csrf_token,利用Jquery进行post请求也需要;否则就会出现403 forbidden错误

2.在Django的view中,如何返回json串给jquery

view方法如下所示:

  1. def get_productitem(request,*callback_args):
  2. productid = request.POST.get('productid')
  3. rtn = dict()
  4. if productid is not None:
  5. productitemlist = Producitem.objects.filter(productid=productid).all()
  6. for productitemitem in productitemlist:
  7. rtn[productitemitem.id] = "%s(%s)"%(productitemitem.itemname,productitemitem.itemversion)
  8. return HttpResponse(json.dumps(rtn),mimetype='application/json')

template中的jquery请求方法如下所示:

  1. $.ajaxSetup({
  2. beforeSend: function(xhr, settings){
  3. var csrftoken = $.cookie('csrftoken');
  4. xhr.setRequestHeader("X-CSRFToken", csrftoken);
  5. }
  6. });
  7. changeProduct();
  8. function changeProduct(){
  9. var productid = $("#productid").val();
  10. if(productid == null){
  11. return;
  12. }
  13. jQuery.post('/getProductitem',{
  14. productid:productid
  15. },function(dat){
  16. var productitemid = $("#productitemid");
  17. var options = '';
  18. for(jsonkey in dat){
  19. options += "<option value='" + jsonkey + "'>" + dat[jsonkey] + "</option>";
  20. }
  21. if(options != ''){
  22. productitemid.html(options);
  23. }
  24. });
  25. }

记得,如果要是用$.cookie方法,需要引入jquery.cookie.js文件。

同时注意在jquery所在页面的form表单里加入{% csrf_token %},否则Jquery post的X-CSRFToken头部为空。

Django配合使用Jquery post方法的更多相关文章

  1. jQuery异步框架探究1:jQuery._Deferred方法

    jQuery异步框架应用于jQuery数据缓存模块.jQuery ajax模块.jQuery事件绑定模块等多个模块,是jQuery的基础功能之中的一个.实际上jQuery实现的异步回调机制能够看做ja ...

  2. jQuery on()方法

    jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map) 由此扩展开来的几个以前常见的方法 ...

  3. jquery ajax 方法及各参数详解

    1.$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息. 参数列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type ...

  4. jquery.extend方法

    jquery.extend()用来扩展jquery中方法,实现插件. 1.jQuery.extend函数详细用法! 扩展jQuery静态方法. 1$.extend({ 2test:function() ...

  5. jQuery extend方法使用及实现

    一.jQuery extend方法介绍 jQuery的API手册中,extend方法挂载在jQuery和jQuery.fn两个不同对象上方法,但在jQuery内部代码实现的是相同的,只是功能却不太一样 ...

  6. 优化加载jQuery的方法

    请看下面的一段代码: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" ...

  7. C#实现jQuery的方法连缀

    jQuery的方法连缀使用起来非常方便,可以简化语句,让代码变得清晰简洁.那C#的类方法能不能也实现类似的功能呢?基于这样的疑惑,研究了一下jQuery的源代码,发现就是需要方法连缀的函数方法最后返回 ...

  8. jQuery原型方法each使用和源码分析

    jQuery.each方法是jQuery的核心工具方法之一,通用例遍方法,可用于例遍对象和数组.不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象.通常需要两个参数 ...

  9. jQuery.clean()方法源码分析(一)

    在jQuery 1.7.1中调用jQuery.clean()方法的地方有三处,第一次就是在我之前的随笔分析jQuery.buildFramgment()方法里面的,其实还是构造函数的一部分,在处理诸如 ...

随机推荐

  1. Unity 重要基础知识点

    这是两个月前的学习记录,发出来了下,如果有误欢迎大家指出: 脚本生命周期 //每当脚本被加载时调用一次 // 1.   在Awake中做一些初始化操作 void Awake(){ //初始化publi ...

  2. utf-8 汉字对照表

    之前从redis中取出一些数据,utf8 16进制编码,想转成字符,没有找到现成的转化工具,先用这个表直接查找对照吧. UTF8编码表大全Code code# Code (coded in UTF-8 ...

  3. Linux进程学习

    进程与进程管理: 清屏:system("clear"); //#include <signal.h> 进程环境与进程属性: 什么是进程:简单的说,进程就是程序的一次执行 ...

  4. C++ 模版

    函数模版 #include <iostream> using namespace std; template<typename T> T add(T t1, T t2) { r ...

  5. [LeetCode] Arithmetic Slices 算数切片

    A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...

  6. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  7. IT培训行业揭秘(三)

    关于培训班的课程是怎么设置的呢? 首先,国内也有几个水平不错的培训机构有自己课程研发体系,有自己的课程研发部门.我一直认为良心培训和黑心培训的区别就在这里,因为学生们所学的知识符不符合市场用功需求,就 ...

  8. underscore 源码解读之 bind 方法的实现

    自从进入七月以来,我的 underscore 源码解读系列 更新缓慢,再这样下去,今年更完的目标似乎要落空,赶紧写一篇压压惊. 前文 跟大家简单介绍了下 ES5 中的 bind 方法以及使用场景(没读 ...

  9. [node.js 学习]1.start a simple server

    1.启动一个本地的服务 下面是官方的例子,会生成一个随机端口,返回的是纯文本: var net = require('net'); var server = net.createServer((soc ...

  10. php 使用函数中遇到的坑之----strpos

    strpos — 查找字符串首次出现的位置 mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) <?ph ...