Have you ever used Google Maps to find your favorite destination? If yes, then you have interacted with AJAX based web application. Following are the top 16 AJAX Interview Questions that are designed for you to crack the interview.
AJAX is the technique of exchanging data with a server and updating parts of a web page without reloading the entire page.
Following are the benefits of AJAX -
It exchanges the data between client and server and the data is in the form of text, XML, or JSON.
In synchronous request, the usual working of a page and user interaction is blocked until a response is received from the server.
In an asynchronous request, the page continues to work normally without blocking the user. It is the most preferred way of sending a request.
There are six properties of XMLHttpRequest-
|onreadystatechange||Use to specify the function name that will be invoked whenever the value of readyState changes.|
|readyState||It contains the status of XMLHttpRequest object. The Range of this property is from 0 to 4.
0- request is not generated
1- connection with the server is made
2- the server receives request
3- request is processing
4- response is received, and request is finished
|responseText||It stores the response data in the form of string.|
|responseXML||It stores the XML data received in response.|
|status||Returns the status code(200 for OK, 403 for Forbidden, 404 for Not Found, and so on)|
|statusText||It holds the text of the status.|
|abort()||This function is used to cancel the current request.|
|getAllResponseHeaders()||It returns all the response headers in the form of a string.|
|getResponseHeader()||This function returns the value of the specified header.|
|open(method,url,async,uname,pswd)||This method is used to initialized the request. It takes the following arguments -
method- the type of HTTP method such as GET, POST, etc.
url- Specifies the file on the server to send the request to.
async- It is optional, and by default it is true. A false value indicates that the request must be handled synchronously and true value indicates asynchronously.
username & password- Optional and by default it is null. Use for authentication purposes.
|send(string)||It sends the request to the server. If the HTTP method of a request is POST, then the argument of this function is not ignored.|
|setRequestHeader(header, value)||This methods must be called before send() method and after open() method. It is used to set the header of HTTP request.|
Following table lists the types of readyState-
|0||request is not generated.|
|1||connection with the server is made.|
|2||the server receives request.|
|3||request is processing.|
|4||response is received, and request is finished.|
If you want to debug in Google Chrome, then use Chrome Developer Tool. On Mozilla Firefox, with the help of Firebug, debugging can be done easily.
Nowadays, many websites and web applications are using AJAX. Some of the most popular web applications are-
Every technology is not a perfect and has some disadvantages. The cons of AJAX are-
An application that works similar to a desktop application and has enhanced the user interface, advanced functions and quicker response time.