Skip to content

Instantly share code, notes, and snippets.

@nonZero
Created December 2, 2015 14:19
Show Gist options
  • Save nonZero/ed3a36bf6f17c3c4466b to your computer and use it in GitHub Desktop.
Save nonZero/ed3a36bf6f17c3c4466b to your computer and use it in GitHub Desktop.
simple ajax demo
$(function () {
"use strict";
$("#update-btn").click(function () {
var el = $(this);
if (el.attr("disabled")) {
return;
}
var url = el.data("url");
el.attr('disabled', "true");
$("#rate").load(url, function (respText, status, xhr) {
if (status == "error") {
$("#rate").css('background', 'red');
}
el.removeAttr('disabled');
}).text("?").css('background', '');
console.log("clicked!");
});
$("#update-cart").click(function () {
var el = $(this);
if (el.attr("disabled")) {
return;
}
var url = el.data("url");
el.attr('disabled', "true");
$.get(url)
.success(function (data) {
$.each(data.items, function (i, item) {
$("<li>").text(item).appendTo($("#items"));
});
$("#total").text(data.total);
})
.error(function () {
alert("something went wrong");
});
console.log("clicked!");
});
$("#send").click(function () {
var el = $(this);
if (el.attr("disabled")) {
return;
}
var url = el.data("url");
el.attr('disabled', "true");
$.post(url, {text: $("#foo").val()})
.success(function (data) {
alert("ok");
})
.error(function () {
alert("NOT OK");
});
console.log("clicked!");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment