ajax calls with jQuery code example

Example 1: ajax call

$.ajax({
        url: "Url",
        dataType: "json",
        type: "Post",
        async: true,
        data: {"Key":value,"Key2":value2},
        success: function (data) {
                
        },
        error: function (xhr, exception, thrownError) {
            var msg = "";
            if (xhr.status === 0) {
                msg = "Not connect.\n Verify Network.";
            } else if (xhr.status == 404) {
                msg = "Requested page not found. [404]";
            } else if (xhr.status == 500) {
                msg = "Internal Server Error [500].";
            } else if (exception === "parsererror") {
                msg = "Requested JSON parse failed.";
            } else if (exception === "timeout") {
                msg = "Time out error.";
            } else if (exception === "abort") {
                msg = "Ajax request aborted.";
            } else {
                msg = "Error:" + xhr.status + " " + xhr.responseText;
            }
            if (callbackError) {
                callbackError(msg);
            }
           
        }
    });

Example 2: make ajax calls with jQuery

// GET Request
    $.ajax({
        url: "example.php?firstParam=Hello&secondParam=World", //you can also pass get parameters
        dataType: 'json',	//dataType you expect in the response from the server
        timeout: 2000
    }).done(function (data, textStatus, jqXHR) {
        //your code here
    }).fail(function (jqXHR, textStatus, errorThrown) {
        console.log("jqXHR:" + jqXHR);
        console.log("TestStatus: " + textStatus);
        console.log("ErrorThrown: " + errorThrown);
    });

//POST Request
    var formData = {name: "John", surname: "Doe", age: "31"}; //Array 
    $.ajax({
        url: "example.php",
        type: "POST", // data type (can be get, post, put, delete)
        data: formData, // data in json format
       	timeout: 2000,	//Is useful ONLY if async=true. If async=false it is useless
        async: false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
        success: function (data, textStatus, jqXHR) {
            //your code here
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("jqXHR:" + jqXHR);
            console.log("TestStatus: " + textStatus);
            console.log("ErrorThrown: " + errorThrown);
        }
    });


//Alternatively, the old aproach is
    $.ajax({
        url: "api.php?action=getCategories",
        dataType: 'json',
        timeout: 2000,
        success: function (result, textStatus, jqXHR) {   //jqXHR = jQuery XMLHttpRequest
            /*You could put your code here but this way of doing it is obsolete. Better to use .done()*/
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("jqXHR:" + jqXHR);
            console.log("TestStatus: " + textStatus);
            console.log("ErrorThrown: " + errorThrown);
        }
    });

Example 3: ajax calls

AJAX is allowing the Web page to retrieve small amounts of data from the
server without reloading the entire page. It is used for creating
fast and dynamic web pages.
There are several Wait methods I use to handle
AJAX calls. I add these pieces of code to my AJAX
testing code to handle it.
1. Explicit Waits
webdriver driver = new firefoxdriver();
driver.get("http://somedomain/url_that_delays_loading");
webelement mydynamicelement = (new webdriverwait(driver,10))
.until(expectedconditions.presenceofelementlocated(by.id("myd
ynamicelement")));
2. Implicit Waits
I also go for Implicit Waits, where I can decide on a
certain amount of time require WebDriver to poll the DOM for.  
 In this case your WebDriver will keep looking for an element(s),
 if it had been unavailable immediately. The default
time is set as 0, which can be easily adjusted as you may prefer.
In addition, this set wait time lasts as long as your browser is
open, so the time to search for any element on the page will be
the same.
webdriver driver = new firefoxdriver();
driver.manage().timeouts().implicitlywait(10,
timeunit.seconds);
driver.get("http://somedomain/url_that_delays_loading");
webelement mydynamicelement =
driver.findelement(by.id("mydynamicelement"));