how to handle ajax calls in selenium

JavaScript
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"));
Source

Also in JavaScript: