Skip to content

Instantly share code, notes, and snippets.

@bradorego
Created November 23, 2013 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradorego/7617364 to your computer and use it in GitHub Desktop.
Save bradorego/7617364 to your computer and use it in GitHub Desktop.
function sendAJAX(url, method, callback, data, headers) {
var xhReq = new XMLHttpRequest();
if (method !== "POST" && method !== "GET" && method !== "PUT" && method !== "DELETE") {
return false;
}
xhReq.open(method, url);
if (typeof(headers) != "undefined") {
for (var i = 0; i < headers.headers.length; i++) {
xhReq.setRequestHeader(headers.headers[i].type, headers.headers[i].value);
}
}
xhReq.onreadystatechange = function () {
if (xhReq.readyState != 4) return;
callback(xhReq);
};
if (xhReq.readyState === 4 ) return xhReq;
xhReq.send(data);
}
var $photoDialog = document.getElementById('photoDialog');
$photoDialog.getElementsByClassName('submit')[0].addEventListener(touchEvent, function (e) {
var url = "/" + planIdea + "/" + planIdeaID + "/images",
formData = new FormData(),
data = {};
formData.append('file', fileObj);
formData.append('text', $photoDialog.getElementsByTagName('textarea')[0].value);
sendAJAX(url, "POST", function (resp) {
if (resp.status === 200) {
alert("Image added!");
window.location.reload();
} else {
alert("An error occurred. HTTP error " + resp.responseText)
}
}, formData);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment