So, how do you create a normal javascript Ajax request without jQuery or any other library?

it is still fairly simple and follows much the same route as with jQuery

The Code Example

function SendAjax() {
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {   // XMLHttpRequest.DONE == 4 // request finished
           if (xmlhttp.status == 200) {
               returned_data = xmlhttp.responseText; // This will contain the data returned
           }
           else if (xmlhttp.status == 400) {
              alert('400 error');
           }
           else {
               alert('something else other than 200 was returned');
           }
        }
    };

    xmlhttp.open("GET", "http://localhost/get_time.php?senddata=mydata", true);// in the url will contain the get paramaters.
    xmlhttp.send();
}

SendAjax()

If you would like to know how to do this with jQuery follow this link http://hobll.com/how-to-jquery-ajax/ I would recommend using jQuery over normal javascript as it will work better with all browsers and scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *