Skip to content

Instantly share code, notes, and snippets.

@zaimramlan
Last active September 17, 2016 14:21
Show Gist options
  • Save zaimramlan/7f36db23f6b38fd2d542cd940b207e73 to your computer and use it in GitHub Desktop.
Save zaimramlan/7f36db23f6b38fd2d542cd940b207e73 to your computer and use it in GitHub Desktop.
Send AJAX Request via JS Function
function ajax_request() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == [READY_STATE] && this.status == [HTTP_RESPONSE_CODE]) {
            // do something
        }
    };
    xhttp.open("GET", [REQUEST_PATH], true);
    xhttp.send();
}

Status of the XMLHttpRequest

Changes from 0 to 4:

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request finished and response is ready

HTTP Response Codes:

200: "OK"

404: "Page not found"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment