form提交
form提交和 纯粹的ajax的提交是有区别的
普通的form提交可在表单元素上加上name="obj.list[0].name" 即可提交到后台
但是$.ajax 提交方式不同了
例如
$.ajax({ type: "post", async:false, url:'http://localhost', data:{ id:8813, name:'9990000', aList: [ { name:'sdf' }, { name:'sdf2' }, { name:'sdf3' } ] } , success: function(data){ // dosomething } })
在network里看看有这样一个信息
Form Data
解决这个方法需要从jquery 解决,这应该算是jquery封装ajax的一个bug
解决方法1 扩展jquery的方法,在$.param的基础上修改一句话即可
代码如下:
(function($) { // copy from jquery.jsvar r20 = /%20/g, rbracket = /\[\]$/; $.extend({ customParam: function( a ) { var s = [], add = function( 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 ); }; // If an array was passed in, assume that it is an array of form elements.if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { // Serialize the form elements jQuery.each( a, function() { add( this.name, this.value ); }); } else { for ( var prefix in a ) { buildParams( prefix, a[ prefix ], add ); } } // Return the resulting serializationreturn s.join( "&" ).replace( r20, "+" ); } }); /* private method*/function buildParams( prefix, obj, add ) { if ( jQuery.isArray( obj ) ) { // Serialize array item. jQuery.each( obj, function( i, v ) { if (rbracket.test( prefix ) ) { // Treat each array item as a scalar. add( prefix, v ); } else { buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, add ); } }); } elseif (obj != null && typeof obj === "object" ) { // Serialize object item.for ( var name in obj ) { buildParams( prefix + "." + name, obj[ name ], add ); } } else { // Serialize scalar item. add( prefix, obj ); } }; })(jQuery);
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05