Created
March 17, 2015 10:48
-
-
Save 13twelve/4feaa1f100a711de428f to your computer and use it in GitHub Desktop.
Ajax helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
charlierose.Helpers.ajax_request = function(options) { | |
/* | |
where options is an object: | |
charlierose.Helpers.ajax_request({ | |
url: '/', | |
type: "POST", | |
data: { param1: "param1value", param2: "param2value" }, | |
requestHeader: { header: "Content-Type", value: "application/x-www-form-urlencoded; charset=UTF-8" }, | |
onSuccess: function(data){ | |
}, | |
onError: function(){ | |
} | |
}); | |
data,requestHeader,onSuccess and onError are optional | |
onSuccess returns the responseText, in whatever format that is. | |
*/ | |
var options = options; | |
var request = new XMLHttpRequest(); | |
options.query_string = ""; | |
if (options.data !== undefined) { | |
options.query_string = charlierose.Helpers.turn_object_to_queryString(options.data); | |
} | |
request.open(options.type, options.url+options.query_string, true); | |
if (options.type === "POST" && options.requestHeader === undefined) { | |
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); | |
} | |
if (options.requestHeader !== undefined && Object.getOwnPropertyNames(options.requestHeader).length > 0) { | |
request.setRequestHeader(options.requestHeader.header, options.requestHeader.value); | |
} | |
// Add a header used by Rails to detect ajax requests | |
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); | |
request.onload = function() { | |
if (request.status >= 200 && request.status < 400){ | |
// Success! | |
if ((typeof options.onSuccess).toLowerCase() === "function") { | |
options.onSuccess.call(this,request.responseText); | |
} | |
} else { | |
console.log("We reached our target server, but it returned an error: "+request.statusText); | |
if ((typeof options.onError).toLowerCase() === "function") { | |
options.onError.call(this,request.responseText); | |
} | |
} | |
}; | |
request.onerror = function() { | |
console.log("There was a connection error of some sort"); | |
if ((typeof options.onError).toLowerCase() === "function") { | |
options.onError.call(); | |
} | |
}; | |
request.send(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
charlierose.Helpers.ajax_request({ | |
url: request_url, | |
requestHeader: { header: "X-CSRF-Token", value: token }, | |
type: "GET", | |
onSuccess: function(data){ | |
// Add new videos | |
document.getElementById(container.attr("data-target")).innerHTML += data; | |
// tell the document about it | |
document.trigger("more_videos_added"); | |
// | |
container.removeClass("btn-loading"); | |
}, | |
onError: function(){ | |
// on error | |
container.parentNode.removeChild(container); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
charlierose.Helpers.turn_object_to_queryString = function(obj) { | |
var query_string = ""; | |
var count = 0; | |
if (Object.getOwnPropertyNames(obj).length > 0) { | |
query_string = "?"; | |
for (key in obj) { | |
if (!obj.hasOwnProperty(key)) { | |
continue; | |
} | |
query_string += ((count > 0) ? "&" : "") + key + "=" + encodeURIComponent(obj[key]).replace(/[!'()]/g, escape).replace(/\*/g, "%2A"); | |
count++; | |
} | |
} | |
return query_string; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment