今天试了一下jquery1.4,我用的版本是jquery-1.4.2.js,在进行ajax传输时发现老是出现乱码,郁闷了好久,网上搜了一下,才知道原来是jquery-1.4中jQuery.param() 方法有一些变化:
参考 http://www.hackhome.com/InfoView/Article_208618_2.html
之前对于{foo: ["bar", "baz"]}序列化后的结果是“foo=bar&foo=baz”,而现在是“foo[]=bar&foo[]=baz”。
见源码5405行-5433行
function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) ) {
// Serialize array item.
jQuery.each( obj, function( i, v ) {
if ( traditional || /\[\]$/.test( prefix ) ) {
// Treat each array item as a scalar.
add( prefix, v );
} else {
// If array item is non-scalar (array or object), encode its
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v );
}
});
} else if ( !traditional && obj != null && typeof obj === "object" ) {
// Serialize object item.
jQuery.each( obj, function( k, v ) {
buildParams( prefix + "[" + k + "]", v );
});
} else {
// Serialize scalar item.
add( prefix, obj );
}
}
这下就知道为什么出现乱码了,因为在jquery源码中jQuery.param()方法是会进行字符编码的,在源码5436行:
function add( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
}
这里的中括号“[”,“]”就变成了“%5B”,“%5D”。
解决的方法有两个
一、既然是编码不对,那就干脆不让他编码,索性把源码中的5439行
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
改为
s[ s.length ] = key + "=" + encodeURIComponent(value);
二、前面方法能解决这个问题,不过修改了jquery源码,可能会有不良影响,那我们还可以在后台使用过滤器进行字符编码处理,这样就ok了。
一点拙见,高手见笑了
分享到:
相关推荐
如果你想要使用旧的序列化方式,有三种设置方法: 代码如下: jQuery.ajaxSettings.traditional = true; jQuery.param( stuff, true ); $.ajax({ data: stuff, traditional: true }); 2 自动检测json和javascript的...
创建适用于URL查询字符串或Ajax请求的数组,普通对象或jQuery对象的序列化表示形式。 如果jQuery对象被传递,它应该包含具有name / value属性的输入元素。 jQuery.param( obj ): obj: 类型:Array或PlainObject或...
页面上jQuery需要把一个复杂类型的javascript数组对象作为参数传到Struts2 action,但是他们的默认格式不匹配导致action无法正确获得参数。 Struts2 action class SomeAction{ List<SomeBean> list; //getter...
$.ajax({ type: p.method, url: p.url, data:param, success: function(msg){ $.AddData(msg,showbox,p); }, error: function(msg){$.ErrorAjax(showbox,p.errorMsg);}, beforeSend:function(){$.AddLoading...
本文首先介绍了干扰问题对基于DSP的数字图像处理系统的危害,其次分析了系统中形成干扰的原因,最后提出了相应的抗干扰技术,并且给出了系统中主要抗干扰措施的设计方案。
script src =" /path/to/jquery-param.min.js " > </ script >用法CommonJS: const param = require ( 'jquery-param' ) ;const obj = { key1 : { value1 : [ 10 , 20 , 30 ] } , key2 : '?a
基于jquery扩展的前端分页插件,只需要将注重点放在后面json数据上,前端只需要遍历表格数据就可完成整个前端的分页操作。 PS:如遇BUG请自行修改 使用请引入css和js $("div").easyPage(url,param,function(data){ $...
在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...
jQuery 1.4.1 速查表 -- Shawphy, 原作:G. Scott Olson 核心 jQuery 核心函数 jQuery(expr, [context]) jQuery(html, [ownerDoc]) jQuery(html, props) jQuery(elements) jQuery() jQuery(callback) jQuery 对象访问...
利用jquery技术和url传参,实现tomcat网页和Webstorm网页之间实现跨域数据传输
一个用于解析jquery.param()字符串的python库。 可以这样使用: >>> from jquery_unparam import jquery_unparam >>> request_params = u'a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3' >>> print(jquery_...
类似 bb.html?data=1&data2=2这种页面跳转,通过该js可以获取参数 方法:在目标页面,即bb.html中使用一下代码即可: var data=$.query.get("data")
此资源包是jQuery.param.js。非常好用的js文件,有需要的请放心下载使用。
在ajax中使用post方法,用常规的参数格式:param1=a1¶m2=a2 ,当参数长度过长时,依然提交不成功。比如我们经常这样写一个ajax的post请求: $.ajax({ type: "post", // post or get contentType:"application...
实际开发中,难免会遇到页面之间跳转传参的需要,该js可以用于html页面跳转传参,会更加方便快捷,几行代码就可以搞定
function httpAPIPost(url,param:string):string; var ResponseStream,PostStream:TStringStream; begin try idslhndlrscktpnsl1.SSLOptions.Method:=sslvSSLv23; idhtp1.HandleRedirects := True; idhtp1.Read...
做ajax的时候,经常需要序列化, serialize()只能序列化表单 。(注 : 表单里的内容 只能用 name ) 现在有一个对象: 代码如下:var obj={a:1,b:2,c:3}; 怎么让它序列化呢? 用prototype 可以用$H(obj)....
使用$.isPlainObject()函数检测对象是否为原始对象 使用$.contains()函数检测两个节点是否包含 使用$.param()进行数组元素序列化 使用函数$.extend()扩展工具函数 使用函数$.proxy()改变事件函数的作用域 使用...
Deparam是jquery的 .param方法的逆函数。 它接受一个参数化的查询字符串,并将其转换回一个对象。 该格式在许多方面都是一种更紧凑的方式,可以通过JSON序列化javascript对象。 例如(从包含的测试中): var ...