Add the option to use FormData to encode the form elements (#153)

* Add the option to use FormData to encode the form elements

If the form's enctype attribute is set to "multipart/form-data",
use FormData to encode the form's elements.
This commit was merged in pull request #153.
This commit is contained in:
BehindTheMath
2018-04-29 15:05:22 -04:00
committed by GitHub
parent 7d26a75fdf
commit e49d8947f7
4 changed files with 54 additions and 7 deletions

View File

@@ -15,8 +15,7 @@ var formAction = function(el, event) {
// Initialize requestOptions
options.requestOptions = {
requestUrl: el.getAttribute("action") || window.location.href,
requestMethod: el.getAttribute("method") || "GET",
requestParams: []
requestMethod: el.getAttribute("method") || "GET"
}
// create a testable virtual link of the form action
@@ -31,6 +30,22 @@ var formAction = function(el, event) {
event.preventDefault()
if (el.enctype === "multipart/form-data") {
options.requestOptions.formData = new FormData(el)
}
else {
options.requestOptions.requestParams = parseFormElements(el)
}
el.setAttribute(attrState, "submit")
options.triggerElement = el
this.loadUrl(virtLinkElement.href, options)
}
function parseFormElements(el) {
var requestParams = []
for (var elementKey in el.elements) {
if (Number.isNaN(Number(elementKey))) {
continue;
@@ -62,7 +77,7 @@ var formAction = function(el, event) {
}
for (var j = 0; j < values.length; j++) {
options.requestOptions.requestParams.push({
requestParams.push({
name: encodeURIComponent(element.name),
value: encodeURIComponent(values[j])
})
@@ -71,10 +86,7 @@ var formAction = function(el, event) {
}
}
el.setAttribute(attrState, "submit")
options.triggerElement = el
this.loadUrl(virtLinkElement.href, options)
return requestParams
}
function checkIfShouldAbort(virtLinkElement, options) {