Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for post json body (fix #545) #557

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ 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:
Expand Down
23 changes: 22 additions & 1 deletion src/jquery.form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -200,9 +201,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the var when declaring optionsData?


options.extraData = optionsData;
qx = $.param(optionsData, traditional);
Expand Down Expand Up @@ -232,7 +234,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'
}

Expand Down Expand Up @@ -1193,6 +1201,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] = 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
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''},
Expand Down