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. MonoBehaviour Lifecycle(生命周期/脚本执行顺序)

    脚本执行顺序 前言 搭建一个示例来验证Unity脚本的执行顺序,大概测试以下部分: 物理方面(Physics) 渲染(Scene rendering) 输入事件(InputEvent) 流程图 Uni ...

  2. [LeetCode] Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  5. Netty学习笔记之一(Netty解析简单的Http Post Json 请求)

    一,HTTP解码器可能会将一个HTTP请求解析成多个消息对象. ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast( ...

  6. 网络切片在5G中的应用

    SDNLAB君 • 16-11-25 •1509 人围观 5G和网络切片 当5G被广泛提及的时候,网络切片是其中讨论最多的技术.像KT.SK Telecom.China Mobile.DT.KDDI. ...

  7. booting logo & booting animation

    開機第一張圖片: 圖片位置: linux_repo/vendor/mediatek/proprietary/bootable/bootloader/lk/dev/logo 因為 project 選用 ...

  8. Linux配置JDK1.7和Resin4.0

    1.安装JDK1.7 (1)下载 官网下载路径:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18802 ...

  9. POI导出Excel并下载

    首先在pom.xml添加jar包: <!-- 导出excel --> <dependency> <groupId>org.apache.poi</groupI ...

  10. PL/SQL数据库,Oracle登录

    用户名:TESTZYPX_9999 数据库:10.75.142.242:1521/orcl