diff --git a/lib/proto/attach-form.js b/lib/proto/attach-form.js index 43ac1a6..3ca881b 100644 --- a/lib/proto/attach-form.js +++ b/lib/proto/attach-form.js @@ -11,7 +11,8 @@ var formAction = function(el, event) { // Initialize requestOptions options.requestOptions = { requestUrl: el.getAttribute("action") || window.location.href, - requestMethod: el.getAttribute("method") || "GET" + requestMethod: el.getAttribute("method") || "GET", + requestParams: [] } // create a testable virtual link of the form action @@ -44,20 +45,20 @@ var formAction = function(el, event) { event.preventDefault() - var params = [] for (var elementKey in el.elements) { var element = el.elements[elementKey] // jscs:disable disallowImplicitTypeConversion if (!!element.name && element.attributes !== undefined && element.tagName.toLowerCase() !== "button") { // jscs:enable disallowImplicitTypeConversion if ((!element.attributes.type || element.attributes.type.value !== "checkbox" && element.attributes.type.value !== "radio") || element.checked) { - params.push({name: encodeURIComponent(element.name), value: encodeURIComponent(element.value)}) + options.requestOptions.requestParams.push({ + name: encodeURIComponent(element.name), + value: encodeURIComponent(element.value) + }) } } } - options.requestOptions.requestParams = params - el.setAttribute(attrClick, "submit") options.triggerElement = el diff --git a/lib/send-request.js b/lib/send-request.js index 58ce7fd..f2817b5 100644 --- a/lib/send-request.js +++ b/lib/send-request.js @@ -2,6 +2,7 @@ var updateQueryString = require("./util/update-query-string"); module.exports = function(location, options, callback) { options = options || {} + var queryString var requestMethod = (options.requestMethod || "GET").toUpperCase() var requestParams = options.requestParams || null var requestPayload = null @@ -29,22 +30,23 @@ module.exports = function(location, options, callback) { // Prepare the request payload for forms, if available if (requestParams && requestParams.length) { + // Build query string + queryString = (requestParams.map(function(param) {return param.name + "=" + param.value})).join("&") + switch (requestMethod) { case "GET": // Reset query string to avoid an issue with repeat submissions where checkboxes that were // previously checked are incorrectly preserved location = location.split("?")[0] - // Build new query string - requestParams.forEach(function(param) { - location = updateQueryString(location, param.name, param.value) - }); - break; + // Append new query string + location += "?" + queryString + break case "POST": - // Build payload string - requestPayload = (requestParams.map(function(param) {return param.name + "=" + param.value})).join("&") - break; + // Send query string as request payload + requestPayload = queryString + break } }