From 63b93fcc27e86de69c2b109ec1dd6704768733b8 Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Thu, 20 Sep 2018 01:18:25 +0800 Subject: [PATCH 1/6] Add support for post json body --- README.md | 6 ++++++ src/jquery.form.js | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bdd5fb93..5e3ef4f3 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,12 @@ An object containing extra data that should be submitted along with the form. ```` data: { key1: 'value1', key2: 'value2' } ```` +### requestFormat +Avaliable value: +* `json` : post request data with json string in post body. + * You may need polyfill `JSON` functionality with old browsers, recommending: https://github.com/douglascrockford/JSON-js/blob/master/json2.js +* `form` : (default) post request body with form http format + ### dataType Expected data type of the response. One of: null, 'xml', 'script', or 'json'. The dataType option provides a means for specifying how the server response should be handled. This maps directly to jQuery's dataType method. The following values are supported: diff --git a/src/jquery.form.js b/src/jquery.form.js index 2a75fe0c..c1629a29 100644 --- a/src/jquery.form.js +++ b/src/jquery.form.js @@ -200,9 +200,10 @@ var elements = []; var qx, a = this.formToArray(options.semantic, elements, options.filtering); + var optionsData; if (options.data) { - var optionsData = $.isFunction(options.data) ? options.data(a) : options.data; + optionsData = $.isFunction(options.data) ? options.data(a) : options.data; options.extraData = optionsData; qx = $.param(optionsData, traditional); @@ -232,7 +233,13 @@ if (options.type.toUpperCase() === 'GET') { options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; options.data = null; // data is null for 'get' + } else if (options.requestFormat.toLowerCase() === 'json') { + var formData = this.formArrayToJsonData(a); + var jsonData = $.extend({}, formData, traditional); + options.data = JSON.stringify(jsonData); + options.contentType = 'application/json'; } else { + // form-data post options.data = q; // data is the query string for 'post' } @@ -1193,6 +1200,19 @@ return $.param(this.formToArray(semantic)); }; + /** + * Transform form array data into json object. + */ + $.fn.formArrayToJsonData = function (arrayOfData) { + var result = {}; + + $.each(arrayOfData, function (index, node) { + result[node.name] = result[node.value]; + }); + + return result; + }; + /** * Serializes all field elements in the jQuery object into a query string. * This method will return a string in the format: name1=value1&name2=value2 From bce82c88ef38fe0d3b3c28b060fcb18ef87c189d Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Thu, 20 Sep 2018 01:26:28 +0800 Subject: [PATCH 2/6] Fix doc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e3ef4f3..50ba1892 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ data: { key1: 'value1', key2: 'value2' } Avaliable value: * `json` : post request data with json string in post body. * You may need polyfill `JSON` functionality with old browsers, recommending: https://github.com/douglascrockford/JSON-js/blob/master/json2.js -* `form` : (default) post request body with form http format +* `form` : (default) post request body with http form (x-www-form-urlencoded) format ### dataType From aa98e3cedd81a6e9f9e9561af4efc8d39999659e Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Thu, 20 Sep 2018 01:31:28 +0800 Subject: [PATCH 3/6] Add default for requestFormat --- src/jquery.form.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/jquery.form.js b/src/jquery.form.js index c1629a29..47199a78 100644 --- a/src/jquery.form.js +++ b/src/jquery.form.js @@ -170,6 +170,7 @@ url : url, success : $.ajaxSettings.success, type : method || $.ajaxSettings.type, + requestFormat: 'form', iframeSrc : /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' // eslint-disable-line no-script-url }, options); From 5a120af54d60818865e33eadecf6fb0d9b8f66a4 Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Sat, 23 Mar 2019 16:06:43 +0800 Subject: [PATCH 4/6] Fix: value error --- src/jquery.form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.form.js b/src/jquery.form.js index 47199a78..8d05fe4f 100644 --- a/src/jquery.form.js +++ b/src/jquery.form.js @@ -1208,7 +1208,7 @@ var result = {}; $.each(arrayOfData, function (index, node) { - result[node.name] = result[node.value]; + result[node.name] = node.value; }); return result; From a8b97c8f7d023f0b80bbaff91cbf6d8bbc2a481a Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Sat, 23 Mar 2019 16:06:56 +0800 Subject: [PATCH 5/6] Add unit test for json post --- test/test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/test.js b/test/test.js index 13bd9431..1be4d838 100644 --- a/test/test.js +++ b/test/test.js @@ -65,6 +65,26 @@ describe('form', function() { } }); + it('formToArray: json object', function() { + var a = $('#form1').formToArray(); + var o = $.fn.formArrayToJsonData(a); + // console.log(JSON.stringify(o,0,4)); + assert.strictEqual(o.constructor, Object, 'type check'); + assert.deepEqual(o, { + "Hidden": "hiddenValue", + "Name": "MyName1", + "Password": "", + "Multiple": "six", + "Single": "one", + "Single2": "A", + "Check": "2", + "Radio": "2", + "action": "1", + "method": "2", + "Text": "This is Form1" + }); + }); + it('formToArray: text promotion for missing value attributes', function() { var expected = [ { name: 'A', value: ''}, From 6a654a0f0d06433c58ca4fc6e586f03a2a9bed72 Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Sat, 23 Mar 2019 16:09:15 +0800 Subject: [PATCH 6/6] Fix: document --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 50ba1892..f33243a0 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,11 @@ An object containing extra data that should be submitted along with the form. data: { key1: 'value1', key2: 'value2' } ```` ### requestFormat -Avaliable value: +Available value: + +* `form` : post request body with http form (x-www-form-urlencoded) format, this is default option. * `json` : post request data with json string in post body. * You may need polyfill `JSON` functionality with old browsers, recommending: https://github.com/douglascrockford/JSON-js/blob/master/json2.js -* `form` : (default) post request body with http form (x-www-form-urlencoded) format ### dataType