Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active December 21, 2016 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehins/e37be1fff5548e790d18 to your computer and use it in GitHub Desktop.
Save mikehins/e37be1fff5548e790d18 to your computer and use it in GitHub Desktop.
Ajax call boiler plate for jQuery
var AJAX = {
post: function (u, d, dt, cb) {
dt = typeof dt !== 'undefined' ? dt : 'json';
return $.ajax({
url: u,
data: d,
type: 'POST',
dataType: dt
});
},
get: function (u, d, dt, cb) {
dt = typeof dt !== 'undefined' ? dt : 'json';
return $.ajax({
url: u,
data: d,
type: 'GET',
dataType: dt
});
},
infos: function () {
var infos = {};
$.get('http://jsonip.com/', function (r) {
infos.user_ip = r.ip;
infos.uagent = navigator.userAgent;
infos.referer = window.location.href;
});
return infos;
}
};
// usage
var getData = function () {
$.when(AJAX.get('/api'))
.then(function (response) {
console.log(response);
});
};
var postData = function () {
var data = {};
$.when(AJAX.post('/api', data))
.then(function (response) {
console.log(response);
});
};
console.log(AJAX.infos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment