`
Folyred
  • 浏览: 57600 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类

jQuery ajax的traditional参数的作用

阅读更多

一般的,可能有些人在一个参数有多个值的情况下,可能以某个字符分隔的形式传递,比如页面上有多个checkbox:
$.ajax{
      url:"xxxx",
      data:{
            p: "123,456,789"
      }
}
然后后台获取参数后再分隔,这种做法有很大弊端,试问如果某一个参数值中出现了分隔符所使用的字符,会出现什么情况呢?当然后台获取的参数将与实际情况不符.
这时我想到了将上述代码写成这样:
$.ajax{
      url:"xxxx",
      data:{
            p: ["123", "456", "789"]
      }
}
如果单纯写成这样,在java后台是无法取到参数的,因为jQuery需要调用jQuery.param序列化参数,
jQuery.param( obj, traditional )
默认的话,traditional为false,即jquery会深度序列化参数对象,以适应如PHP和Ruby on Rails框架,
但servelt api无法处理,我们可以通过设置traditional 为true阻止深度序列化,然后序列化结果如下:
p: ["123", "456", "789"]    =>    p=123&p=456&p=456
随即,我们就可以在后台通过request.getParameterValues()来获取参数的值数组了,
所以,比如我们前台有多个checkbox,前台的代码可以写成:
var values = $("input[type=checkbox]").map(function(){
      return $(this).val();
}).get();
$.ajax{
      url:"xxxx",
      traditional: true,
      data:{
            p: values
      }
}


代码是不是优美并且稳健了许多呢?

转:http://www.codeloger.com/?p=326

 

分享到:
评论
2 楼 Fs_sky 2014-07-15  
不错,学习了!
1 楼 javaloverZY 2012-12-09  
        

相关推荐

    jQuery 1.4.1 中文参考

    11.6.1 jQuery.param(obj, [traditional]) 194 11.7 插件编写 196 11.7.1 jQuery.error(message) 196 12. 关于 197 12.1 关于jQuery 中文文档 197 12.2 关于jQuery 1.3 版翻译 197 12.3 关于jQuery 1.2 版翻译 197 ...

    jquery.param.patch.zip

    页面上jQuery需要把一个复杂类型的javascript数组对象作为参数传到Struts2 action,但是他们的默认格式不匹配导致action无法正确获得参数。 Struts2 action class SomeAction{ List<SomeBean> list; //getter...

    jQuery 1.5 API 中文版

    strjQuery.param( obj, [traditional] ) Utilities Browser and Feature Detection objjQuery.support objjQuery.browserdeprecated strjQuery.browser.versiondeprecated booljQuery.boxModeldeprecated Basic ...

    jquery1.4 教程二 ajax方法的改进

    1 允许序列化嵌套的参数。 比如:{foo: [“bar”, “baz”]} 将被序列化成... $.ajax({ data: stuff, traditional: true }); 2 自动检测json和javascript的数据类型。 1.4后返回json或javascript可以不用设置dataType

    Web Development with jQuery(Wrox,2015)

    The book focuses on the new features of jQuery that improve performance and speed, providing huge advantages over traditional JavaScript. This resource provides a deep understanding of jQuery and ...

    jquery1.11.0手册

    jQuery 1.11.0 速查表 核心 jQuery 核心函数 jQuery([sel,[context]]) jQuery(html,[ownerDoc])1.8* jQuery(callback) jQuery.holdReady(hold) jQuery 对象访问 each(callback) size() length selector ...

    AJAX and PHP.pdf

    implements traditional techniques with added AJAX flavor, thereby making the form more user-friendly, responsive, and pleasing. The intention of this chapter isn't to build the perfect validation ...

    jquery.param()实现数组或对象的序列化方法

    创建适用于URL查询字符串或Ajax请求的数组,普通对象或jQuery对象的序列化表示形式。 如果jQuery对象被传递,它应该包含具有name / value属性的输入元素。 jQuery.param( obj ): obj: 类型:Array或PlainObject或...

    jQuery1.4 API

    [queueName]) 设置 jQuery.fx.off Ajax Ajax 请求 $.ajax([options]) load(url, [data], [callback]) $.get(url, [data], [fn], [type]) $.getJSON(url, [data], [fn]) $.getScript(url, [callback]) $.post(url, ...

    JavaScript JSON Cookbook(PACKT,2015)

    Then, you'll learn how to use JSON in simple AJAX applications using AngularJS and jQuery. Next, you will learn how to exchange objects with databases using MongoDB and CouchDB. You'll also explore ...

    JavaScript.JSON.Cookbook.1785286900

    Chapter 4 : Using JSON in AJAX Applications with jQuery and AngularJS Chapter 5 : Using JSON with MongoDB Chapter 6 : Using JSON with CouchDB Chapter 7 : Using JSON in a Type-Safe Manner Chapter 8 : ...

    python入门到高级全栈工程师培训 第3期 附课件代码

    06 Ajax功能之dataType和traditional 第58章 01 今日内容概要 02 Ajax补充之serialize 03 分页功能介绍 04 分页基础知识 05 Django内置分页 06 扩展Django内置分页 07 自定义分页组件 08 DjangoForm组件初识 第59...

    Learning.JavaScript.Add.Sparkle.and.Life.to.Your.Web.Pages.3rd.Edition.14

    This book also gives equal preference to JavaScript’s use as a "traditional" language (with filesystem and console access) and to its use as a browser-based language. Learn the latest in JavaScript...

    ExtAspNet_v2.3.2_dll

    ExtAspNet - ExtJS based ASP.NET Controls with Full AJAX Support ExtAspNet是一组专业的Asp.net控件库,拥有原生的AJAX支持和丰富的UI效果, 目标是创建没有ViewState,没有JavaScript,没有CSS,没有...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -ExtAspNet支持原生的AJAX,也就是说控件的属性改变在AJAX过程中会反映到页面中,但并不是所有的控件属性都支持AJAX改变。 -加载s.gif图片在本机进行,不会请求extjs.com远程资源(feedback:efrigate43,abaocoole)...

    simple-tags

    * Updated Traditional Chinese translation (thank to Neil Lin) * Fix a bug with selector taxonomy and user taxonomy. (thank to Nicolas Furno) * Version 1.7.4.1 : * Updated italian translation * Fix...

Global site tag (gtag.js) - Google Analytics