-
Notifications
You must be signed in to change notification settings - Fork 10
/
submitAsJson.js
36 lines (29 loc) · 1.03 KB
/
submitAsJson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Submit HTML form as function
Created by Keith Hackbarth 2018
Based off idea by David Tomaschik
https://systemoverlord.com/2016/08/24/posting-json-with-an-html-form.html
Example usage:
$("form").submit(function(event) {
event.preventDefault();
submitAsJSON(this);
});
*/
/* Function takes a jquery form
and converts it to a JSON dictionary */
function convertFormToJSON(form){
var array = $(form).serializeArray();
var json = {};
$.each(array, function() {
json[this.name] = this.value || '';
});
return json;
}
/* Adds a new form to the body of a page with the JSON data encoded into it */
function submitAsJSON(form) {
var JSONString = JSON.stringify(convertFormToJSON(form)),
HackyJSONString = JSONString.slice(0, -1) + ', "trash": "';
var hackyForm = "<form method='POST' enctype='text/plain' action='" + $(form).attr('action') + "'>" +
"<input name='" + HackyJSONString + "' value='\"}'>" +
"</form>"
$(hackyForm).appendTo('body').submit();
}