Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ajax = {};
- ajax.send = function(url, cb_success, method, data, sync, cb_error) {
- var x = new XMLHttpRequest();
- x.open(method, url, sync);
- x.onreadystatechange = function() {
- if (x.readyState === 4 && x.status === 200) {
- cb_success(x.responseText);
- } else {
- cb_error(x.statusText);
- }
- };
- if (method === 'POST') {
- x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
- }
- x.send(data)
- };
- ajax.get = function(url, data, cb_success, sync, cb_error) {
- var query = [];
- for (var key in data) {
- query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
- }
- ajax.send(url + '?' + query.join('&'), cb_success, 'GET', null, sync, cb_error);
- };
- ajax.post = function(url, data, cb_success, sync, cb_error) {
- var query = [];
- for (var key in data) {
- query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
- }
- ajax.send(url, cb_success, 'POST', query.join('&'), sync, cb_error);
- };
Advertisement
Add Comment
Please, Sign In to add comment