$.ajax({
url: '...'
}).done(function (data) {
console.log('Успех');
}).fail(function (data) {
console.log('Провал');
}).always(function (data) {
console.log('А мне пофигу');
});
var paramPamPam = {
data: null,
defer: $.Deferred(),
get: function () {
if (this.data) {
this.defer.resolve(this.data);
} else {
if (!this.transport) {
this.transport = $.ajax({
url: '...',
type: 'post'
});
}
this.transport
.done(this.enable.bind(this))
.fail(this.disable.bind(this))
.always(function () {
delete this.transport;
}.bind(this));
};
return this.defer.promise();
},
enable: function (data) {
this.data = data;
this.defer.resolve(data);
},
disable: function () {
this.defer.resolve();
}
}
paramPamPam.get().then(function (data) {
console.log(data);
});
paramPamPam.get().then(function (data) {
console.log(data);
});
document.addEventListener('mousewheel', cancel, true); // webkit + IE
document.addEventListener('DOMMouseScroll', cancel, true); // Самые древние FF
document.addEventListener('MozMousePixelScroll', cancel, true); // FF с версии 3.5
document.addEventListener('wheel', cancel, true); // FF с версии 31
function cancel (e) {
e.stopPropagation();
}
<form action="" data-action="chat">
<input type="hidden" name="user" value="Вася">
<textarea name="message" cols="30" rows="10"></textarea>
<br>
<button type="submit">Отправить</button>
<div data-container="response"></div>
</form>
document.addEventListener('keydown', function (e) {
if (e.ctrlKey && e.keyCode == 13 && e.target.name == 'message') {
e.preventDefault();
sendMessage(e.target.form);
}
});
document.addEventListener('submit', function (e) {
if (e.target.dataset && e.target.dataset.action == 'chat') {
e.preventDefault();
sendMessage(e.target);
}
});
function sendMessage (form) {
var sendData = [];
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].name && form.elements[i].value) {
sendData.push(form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value));
}
}
if (sendData.length && form.message.value) {
var request = new XMLHttpRequest();
request.addEventListener('readystatechange', function (data) {
if (this.readyState == 4 && this.status >= 200 && this.status < 400) {
if (!form.response) {
form.response = form.querySelector('[data-container="response"]');
}
form.message.value = '';
//form.response.innerHTML = data.responseText;
form.response.innerHTML = 'Отправлено<br>' + sendData.join('&');
}
});
request.open('POST', form.action);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send(sendData.join('&'));
}
}
$(document).on('click', '.today-date', function () {
var item = $(this);
toggle();
if (item.hasClass('today-date-active')) {
$(document).one('click', toggle);
}
function toggle (e) {
item.toggleClass('today-date-active');
item.closest('.date-lst').toggleClass('date-lst-active');
}
});
var b = 28,
polos = $('.polos');
$(document).keydown(function (e) {
switch (e.keyCode) {
case 38:
b -= 19;
break;
case 40:
b += 19;
break;
}
polos.css({
marginTop: b + 'px'
});
});
$(document).on("submit", 'form', function (e) {
this.target = 't' + Date.now();
});
el1 ~ el2 {...}
Deprecation Notice:
The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
new Promise(send).then(
function (data) {
// И вот тут получаем данные
},
function () {
// Провал
}
);
function send (metod, url, value, header) {
var p = new XMLHttpRequest(),
body = null;
if (metod && metod.toLoweCase() == 'post') {
body = value;
p.setRequestHeader('Content-Type', header || 'application/x-www-form-urlencoded');
}
p.open(metod, url);
p.send(body);
p.addEventListener('readystatechange', function() {
if (this.readyState === this.DONE && this.status >=200 && this.status < 400) {
resolve(JSON.parse(this.responseText));
} else {
reject();
}
});
}
send();
function send (metod, url, value, header) {
$.ajax({
url: url,
type: metod,
data: value,
dataType: 'json'
}).done(function (data) {
// И вот тут получаем данные
}).error(function () {
// Провал
});
}