comment 0

Ajax simplest example

Ajax simplest example.

Created an input with type=”submit”. Now on button click do e.preventDefault(); this will not let the input to submit(as its default action will refresh the page). We don’t want it to refresh.

After that $.ajax is a jQuery ajax function which will asynchronous update input value to “Ajax success!” in case of success and “Ajax error!” in case of failure. Page will not refresh. Enjoy!

Comments are welcome.

HTML

<form ajax="true">
    <input type="submit" value="Before Ajax!" />
</form>

jQuery – Ajax

$(document).ready(function (e) {
    $("input").on('click', function (e) {
        e.preventDefault();
        $.ajax({
            success: function () {
                $("input").val("Ajax success!");
            },
            error: function() {
                $("input").val("Ajax error!");
            }
        });
    });
});

Ajax simplest example in fiddle