Skip to content

Commit

Permalink
Patch ajax response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Apr 27, 2017
1 parent 26b6912 commit 6cb220e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 93 deletions.
88 changes: 42 additions & 46 deletions build/jax.5.5.0.js
Expand Up @@ -7,7 +7,7 @@
* @copyright Copyright (c) 2009-2017 NOLA Interactive, LLC. (http://www.nolainteractive.com)
* @license http://www.jaxjs.org/license New BSD License
* @version 5.5.0
* @build Apr 6, 2017 10:03:00
* @build Apr 27, 2017 12:13:57
*/
(function(window){
window.jax = {
Expand Down Expand Up @@ -100,19 +100,16 @@
*/
(function(window){
window.jax.http = {
current : 0,
requests : [],
responses : [],
response : null,
methods : [
'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'DELETE'
],
methods : ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'],

/** Function to send AJAX requests */
send : function(url, opts) {
// Create a new request object.
window.jax.http.current = window.jax.random(100000, 999999);
window.jax.http.requests[window.jax.http.current] = (window.XMLHttpRequest) ? new XMLHttpRequest() : false;
var currentIndex = window.jax.random(100000, 999999);
window.jax.http.requests[currentIndex] = (window.XMLHttpRequest) ? new XMLHttpRequest() : false;

// Get options
var method = ((opts != undefined) && (opts.method != undefined)) ? opts.method.toUpperCase() : 'GET';
Expand All @@ -127,53 +124,52 @@
var trace = ((opts != undefined) && (opts.trace != undefined)) ? opts.trace : null;
var fields = ((opts != undefined) && (opts.fields != undefined)) ? opts.fields : false;

if ((window.jax.http.methods.indexOf(method) != -1) && (window.jax.http.requests[window.jax.http.current])) {
if ((window.jax.http.methods.indexOf(method) != -1) && (window.jax.http.requests[currentIndex])) {
if ((method == 'GET') || (method == 'HEAD') || (method == 'OPTIONS')) {
url += '?' + data;
data = null;
}

// Open request
if ((username != null) && (password != null)) {
window.jax.http.requests[window.jax.http.current].open(method, url, async, username, password);
window.jax.http.requests[currentIndex].open(method, url, async, username, password);
} else {
window.jax.http.requests[window.jax.http.current].open(method, url, async);
window.jax.http.requests[currentIndex].open(method, url, async);
}

if (data != null) {
if ((method == 'GET') || (method == 'HEAD') || (method == 'OPTIONS')) {
url += '?' + data;
data = null;
} else if ((method == 'POST') || (method == 'PUT') || (method == 'DELETE')) {
if (data.constructor != FormData) {
window.jax.http.requests[window.jax.http.current].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if (data.length != undefined) {
window.jax.http.requests[window.jax.http.current].setRequestHeader('Content-Length', data.length.toString());
}
window.jax.http.requests[window.jax.http.current].setRequestHeader('Connection', 'close');
if ((data != null) && ((method == 'POST') || (method == 'PUT') || (method == 'PATCH') || (method == 'DELETE'))) {
if (data.constructor != FormData) {
window.jax.http.requests[currentIndex].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if (data.length != undefined) {
window.jax.http.requests[currentIndex].setRequestHeader('Content-Length', data.length.toString());
}
window.jax.http.requests[currentIndex].setRequestHeader('Connection', 'close');
}
}

// Add progress
if (progress != null) {
if (((method == 'GET') || (method == 'HEAD') || (method == 'OPTIONS')) &&
(window.jax.http.requests[window.jax.http.current].onprogress != undefined)) {
window.jax.http.requests[window.jax.http.current].onprogress = progress;
} else if (((method == 'POST') || (method == 'PUT') || (method == 'DELETE')) &&
(window.jax.http.requests[window.jax.http.current].upload != undefined)) {
window.jax.http.requests[window.jax.http.current].upload.addEventListener('progress', progress, false);
(window.jax.http.requests[currentIndex].onprogress != undefined)) {
window.jax.http.requests[currentIndex].onprogress = progress;
} else if (((method == 'POST') || (method == 'PUT') || (method == 'PATCH') || (method == 'DELETE')) &&
(window.jax.http.requests[currentIndex].upload != undefined)) {
window.jax.http.requests[currentIndex].upload.addEventListener('progress', progress, false);
}
}

// If additional headers are set, send them
if (headers != null) {
for (var header in headers) {
window.jax.http.requests[window.jax.http.current].setRequestHeader(header, headers[header]);
window.jax.http.requests[currentIndex].setRequestHeader(header, headers[header]);
}
}

// If status function handlers have been set, the set the onreadystatechange
if (status != null) {
window.jax.http.requests[window.jax.http.current].onreadystatechange = function() {
if (window.jax.http.requests[window.jax.http.current].readyState == 4) {
var response = (window.jax.http.responses[window.jax.http.current] == undefined) ?
window.jax.http.processResponse(window.jax.http.current) : window.jax.http.responses[window.jax.http.current];
window.jax.http.requests[currentIndex].onreadystatechange = function() {
if (window.jax.http.requests[currentIndex].readyState == 4) {
var response = window.jax.http.processResponse(currentIndex);

if (status[response.status] != undefined) {
if (typeof status[response.status] == 'function') {
Expand Down Expand Up @@ -204,14 +200,14 @@
}

// Send the request
window.jax.http.requests[window.jax.http.current].send(data);
window.jax.http.requests[currentIndex].send(data);

var response = (window.jax.http.responses[window.jax.http.current] == undefined) ?
window.jax.http.processResponse(window.jax.http.current) : window.jax.http.responses[window.jax.http.current];
var response = (window.jax.http.responses[currentIndex] == undefined) ?
window.jax.http.processResponse(currentIndex) : window.jax.http.responses[currentIndex];

return window.jax.http.parseResponse(response, type, async, trace, fields);
} else {
throw (!window.jax.http.requests[window.jax.http.current]) ?
throw (!window.jax.http.requests[currentIndex]) ?
'Error: Could not create a request object.' :
'Error: That request method was not allowed.';
}
Expand Down Expand Up @@ -548,10 +544,10 @@

/** Function to process a response */
window.jax.http.processResponse = function(index) {
window.jax.http.responses[index] = {};
var response = {};

if (window.jax.http.requests[index] != undefined) {
window.jax.http.responses[index] = {
response = {
headers : {},
url : '',
status : 0,
Expand All @@ -568,20 +564,20 @@
var head = h[i].substring(0, h[i].indexOf(':')).trim();
var val = h[i].substring(h[i].indexOf(':') + 1).trim();
if (head != '') {
window.jax.http.responses[index].headers[head] = val;
response.headers[head] = val;
}
}
}
window.jax.http.responses[index].url = (typeof window.jax.http.requests[index].responseURL != 'undefined') ? window.jax.http.requests[index].responseURL : '';
window.jax.http.responses[index].status = window.jax.http.requests[index].status;
window.jax.http.responses[index].statusText = window.jax.http.requests[index].statusText;
window.jax.http.responses[index].timeout = window.jax.http.requests[index].timeout;
window.jax.http.responses[index].body = (typeof window.jax.http.requests[index].response != 'undefined') ? window.jax.http.requests[index].response : '';
window.jax.http.responses[index].text = (typeof window.jax.http.requests[index].responseText != 'undefined') ? window.jax.http.requests[index].responseText : '';
window.jax.http.responses[index].xml = (typeof window.jax.http.requests[index].responseXML != 'undefined') ? window.jax.http.requests[index].responseXML : '';
response.url = (typeof window.jax.http.requests[index].responseURL != 'undefined') ? window.jax.http.requests[index].responseURL : '';
response.status = window.jax.http.requests[index].status;
response.statusText = window.jax.http.requests[index].statusText;
response.timeout = window.jax.http.requests[index].timeout;
response.body = (typeof window.jax.http.requests[index].response != 'undefined') ? window.jax.http.requests[index].response : '';
response.text = (typeof window.jax.http.requests[index].responseText != 'undefined') ? window.jax.http.requests[index].responseText : '';
response.xml = (typeof window.jax.http.requests[index].responseXML != 'undefined') ? window.jax.http.requests[index].responseXML : '';
}

window.jax.http.response = window.jax.http.responses[index];
window.jax.http.response = response;
return window.jax.http.response;
};
})(window);
Expand Down

0 comments on commit 6cb220e

Please sign in to comment.