From 63b93fcc27e86de69c2b109ec1dd6704768733b8 Mon Sep 17 00:00:00 2001 From: zhangciwu Date: Thu, 20 Sep 2018 01:18:25 +0800 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] 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/9] 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/9] 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 From 6c9235ee230e4d4b43f879d06bf23304a9fe5d37 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sat, 30 Mar 2019 16:56:22 -0400 Subject: [PATCH 7/9] grunt: Disables Growl --- Gruntfile.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Gruntfile.js b/Gruntfile.js index a8916fb8..bf00840e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -29,7 +29,8 @@ module.exports = function(grunt) { src: ['test/*.html'], }, options: { - run: true + run: true, + growlOnSuccess: false } }, From 95993ee4cd09d07d4bcf8c10569cfcaf61ea1e9b Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sat, 30 Mar 2019 16:56:42 -0400 Subject: [PATCH 8/9] Updates documentation --- README.md | 14 +++++++------- docs/options.md | 12 ++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f33243a0..fc27bf37 100644 --- a/README.md +++ b/README.md @@ -125,13 +125,6 @@ An object containing extra data that should be submitted along with the form. ```` data: { key1: 'value1', key2: 'value2' } ```` -### requestFormat -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 - ### 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: @@ -174,6 +167,13 @@ The HTTP method to use for the request (e.g. 'POST', 'GET', 'PUT'). ### replaceTarget Optionally used along with the the target option. Set to true if the target should be replaced or false if only the target contents should be replaced. +### requestFormat +The encoding format to use for the request body. The following values are supported: + +* `form` : HTTP form (x-www-form-urlencoded) format, this is default option. +* `json` : JSON encoded string + * You may need polyfill JSON functionality with old browsers, recommending: https://github.com/douglascrockford/JSON-js/blob/master/json2.js + ### resetForm Boolean flag indicating whether the form should be reset if the submit is successful diff --git a/docs/options.md b/docs/options.md index 7c6b6874..6833b1b7 100644 --- a/docs/options.md +++ b/docs/options.md @@ -91,6 +91,18 @@ The HTTP method to use for the request (e.g. 'POST', 'GET', 'PUT'). **(version a Default: `false` Optionally used along with the the [`target`](#target) option. Set to `true` if the target should be replaced or `false` if only the target _contents_ should be replaced. **(version added: 2.43)** + +### requestFormat +Default: `form` +The encoding format to use for the request body. **(version added: 4.3.0)** +The following values are supported: + +**'form'**: HTTP form (x-www-form-urlencoded) format + +**'json'**: JSON encoded string + * You may need polyfill JSON functionality with old browsers, recommending: https://github.com/douglascrockford/JSON-js/blob/master/json2.js + + ### resetForm Default: `null` Boolean flag indicating whether the form should be reset if the submit is successful From 25cdb7a5531301a76c079a8a5526ed4bc3d8a68e Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Sat, 30 Mar 2019 17:13:45 -0400 Subject: [PATCH 9/9] test: Formatting --- test/test.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/test.js b/test/test.js index 316381ce..ba6a9b19 100644 --- a/test/test.js +++ b/test/test.js @@ -68,20 +68,20 @@ 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" + 'Hidden': 'hiddenValue', + 'Name': 'MyName1', + 'Password': '', + 'Multiple': 'six', + 'Single': 'one', + 'Single2': 'A', + 'Check': '2', + 'Radio': '2', + 'action': '1', + 'method': '2', + 'Text': 'This is Form1' }); });