zygimantus
7/9/2015 - 12:23 PM

JS AJAX examples

JS AJAX examples

/**
 * Will be called if etc. <button id="showSomething" class="btn btn-primary" onclick="showSomething()">Show</button>
 * was clicked
 * 
 * @returns {undefined}
 */
function showSomething() {
    $.ajax({
        url: window.location.href + "/showSomething",
        type: "GET",
        dataType: 'json',
        success: function (data) {
            $("#something").removeClass('hidden');  // something will be visible
            $("#something").text(data);             // something will have a text (if it is <b> tag etc.)
            $("#showSomething").remove();           // button will be removed
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText);                // show error
        }
    });
}