wx.request 普通表单无法提交数组
‘content-type’: ‘application/x-www-form-urlencoded; charset=UTF-8’
当使用表单方式提交的时候,无法像浏览器一样传输数组。希望尽快修复。
wx.request({ url: xxx, //仅为示例,并非真实的接口地址 data: { "rs" : "login" , "rsargs" : [ 'aaa' , 1 ] }, method: "POST" , header: { 'content-type' : 'application/x-www-form-urlencoded' }, success: function (res) { } }) |
请求的格式是下面这样的
正常浏览器是这样的
2 回复
估计你和我遇到的问题是一样的。
解决方法,使用jquery的param方法 包装参数就好了
- header配置为 x-www-formxxxxx…
- 包装参数
reqData = this._param(reqData);
其中 _param 就是 jquery的$.param方法 copy过来就好。
_param: function (a, traditional) { var class2type = {}; var toString = class2type.toString; var hasOwn = class2type.hasOwnProperty; function toType(obj) { if (obj == null ) { return obj + "" ; } // Support: Android <=2.3 only (functionish RegExp) return typeof obj === "object" || typeof obj === "function" ? class2type[toString.call(obj)] || "object" : typeof obj; } var isFunction = function isFunction(obj) { // Support: Chrome <=57, Firefox <=52 // In some browsers, typeof returns "function" for HTML <object> elements // (i.e., `typeof document.createElement( "object" ) === "function"`). // We don't want to classify *any* DOM node as a function. return typeof obj === "function" && typeof obj.nodeType !== "number" ; }; var rbracket = /\[\]$/, rCRLF = /\r?\n/g, rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i, rsubmittable = /^(?:input|select|textarea|keygen)/i; function buildParams(prefix, obj, traditional, add) { var name; if (Array.isArray(obj)) { // Serialize array item. obj.forEach( function (v, i) { if (traditional || rbracket.test(prefix)) { // Treat each array item as a scalar. add(prefix, v); } else { // Item is non-scalar (array or object), encode its numeric index. buildParams( prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]" , v, traditional, add ); } }); } else if (!traditional && toType(obj) === "object" ) { // Serialize object item. for (name in obj) { buildParams(prefix + "[" + name + "]" , obj[name], traditional, add); } } else { // Serialize scalar item. add(prefix, obj); } } // Serialize an array of form elements or a set of // key/values into a query string var param = function (a, traditional) { var prefix, s = [], add = function (key, valueOrFunction) { // If value is a function, invoke it and use its return value var value = isFunction(valueOrFunction) ? valueOrFunction() : valueOrFunction; s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value == null ? "" : value); }; // If an array was passed in, assume that it is an array of form elements. if (Array.isArray(a)) { // Serialize the form elements a.forEach( function (item) { add(item.name, item.value); }); } else { // If traditional, encode the "old" way (the way 1.3.2 or older // did it), otherwise encode params recursively. for (prefix in a) { buildParams(prefix, a[prefix], traditional, add); } } // Return the resulting serialization return s.join( "&" ); }; return param(a, traditional); }, |
偷个懒 直接copy粘贴过来 试试吧,我这边测试没有问题