function ajaxRequest(method, url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(null, JSON.parse(xhr.responseText));
} else {
callback(new Error('Request failed with status ' + xhr.status));
}
}
};
xhr.open(method, url, true);
if (method === 'POST' || method === 'PUT') {
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.send(data ? JSON.stringify(data) : null);
}
ajaxRequest('POST', 'https://api.vid.com/recommend', { limit: 2 }, function (err, response) {
if (err) {
console.error(err);
} else {
console.log(response);
}
});
ajaxRequest('GET', 'https://api.vid.com/recommend', null, function (err, response) {
if (err) {
console.error(err);
} else {
console.log(response);
}
});