Create generic request javascript method
parent
728ea6b3ee
commit
510bf65dcd
|
@ -1,3 +1,13 @@
|
|||
function request(method, url, data, loadevent) {
|
||||
var req = new XMLHttpRequest();
|
||||
req.open(method, url);
|
||||
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
req.addEventListener('load', loadevent);
|
||||
|
||||
req.send(data)
|
||||
}
|
||||
|
||||
function initButtons() {
|
||||
var buttons = document.querySelectorAll('button[data-action=remove-movie]');
|
||||
for (var i = 0; i < buttons.length; i++) {
|
||||
|
@ -38,45 +48,27 @@ function search(value) {
|
|||
var url = '/search?search=' + encodeURIComponent(value)
|
||||
window.history.pushState({value: value}, "", url);
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('GET', url);
|
||||
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
req.addEventListener('load', function(e) {
|
||||
request('GET', url, null, function(e) {
|
||||
document.getElementById('movies').parentNode.innerHTML = this.responseText;
|
||||
});
|
||||
|
||||
req.send()
|
||||
}
|
||||
|
||||
function removeMovie(id) {
|
||||
var data = new FormData();
|
||||
data.append('id', id);
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('POST', '/remove');
|
||||
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
req.addEventListener('load', function(e) {
|
||||
request('POST', '/remove', data, function(e) {
|
||||
document.querySelector('.movie[data-id="' + id + '"]').parentNode.remove();
|
||||
});
|
||||
|
||||
req.send(data)
|
||||
}
|
||||
|
||||
function viewMovie(id) {
|
||||
var data = new FormData();
|
||||
data.append('id', id);
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
req.open('POST', '/view');
|
||||
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
req.addEventListener('load', function(e) {
|
||||
request('POST', '/view', data, function(e) {
|
||||
document.querySelector('.movie[data-id="' + id + '"] button[data-action="view-movie"]').parentNode.remove();
|
||||
});
|
||||
|
||||
req.send(data)
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function(event) {
|
||||
|
|
Loading…
Reference in New Issue