Last active
December 21, 2016 10:25
-
-
Save mikehins/e37be1fff5548e790d18 to your computer and use it in GitHub Desktop.
Ajax call boiler plate for jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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