Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ajax = (function(){
- var _ = {};
- /* options = {success: function(request) {...}, fail: function(code, text) {...} } */
- _.plain = function(method, url, options, params) {
- options = options || {};
- var req = new XMLHttpRequest;
- if (options.success || options.fail) {
- req.onreadystatechange = function() {
- if (req.readyState == 4) {
- if (req.status >= 200 && req.status < 300) {
- if (options.success) {
- options.success(req);
- }
- return;
- }
- if (options.fail) {
- options.fail(req.status, req.statusText);
- }
- }
- }
- }
- req.open(method, url);
- if (method.toUpperCase() == 'POST') {
- req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- params = buildQueryString(params);
- }
- if (options.before) {
- options.before(req);
- }
- req.send(params || null);
- }
- _.get = function(url, options) {
- _.plain('GET', url, options, null);
- }
- _.post = function(url, params, options) {
- _.plain('POST', url, options, params);
- }
- return _;
- })();
Advertisement
Add Comment
Please, Sign In to add comment