This tutorial provides frequently asked Ajax interview questions and answers with explanations to help you prepare for the Ajax interview:
In a typical web application, the client, which is the web browser, sends a request to the server, the server processes the request and sends a response back to the client in HTML format. Thus it communicates and shares data with the help of HTTP protocol over the network.
This Request/Response model has limitations, such as HTTP is a stateless protocol, request/response exchange for a small operation like selecting items from the drop-down list drains out the bandwidth.
Hence new technology such as Ajax was invented to send and receive data asynchronously without reloading the web pages.
Only valuable data from the client browser communicates to receive specific data from the server back to the browser, making your application quick, interactive, displaying data for a specific portion of the page without reloading the entire page.
Click here for the official developer guide to know in-depth about Ajax and other technologies.
Table of Contents:
- Introduction To Ajax
- Most Popular Ajax Interview Questions
- Q #1) How do Synchronous and Asynchronous Requests differ?
- Q #2) List the difference between JavaScript and Ajax
- Q #3) Describe Ajax.
- Q #4) List Ajax features.
- Q #5) List advantages of using Ajax in web development
- Q #6) What are the limitations of Ajax?
- Q #7) List Ajax frameworks.
- Q #8) Explain the working of Ajax.
- Q #9) What are the security threats that prevail with Ajax code?
- Q #10) What is XMLHttpRequest?
- Q #11) List various properties of XMLHttpRequest.
- Q #12) Describe various methods used in XMLHttpRequest
- Q #13) Explain various ways open() method can be used for XMLHttpRequest.
- Q #14) Explain Ajax callback function.
- Q #15) List the steps to improve Ajax performance
- Q #16) What are the ready state of requests used with Ajax requests?
- Q # 17) Describe events and their types in Ajax
- Q #18) List Ajax controls in ASP.Net
- Q #19) Explain UpdatePanel Control of ASP.Net
- Q #20) Describe ScriptManager in ASP.Net
- Q #21) What is the difference between ScriptManager and ScriptManagerProxy?
- Q #22) How to call Ajax from JavaScript?
- Q #23) List the tools to debug Ajax applications.
- Q #24) Describe the use of Cache false in Ajax call.
- Q #25) How would you identify the completion of Ajax’s request?
- Q #26) Explain the importance of client-side libraries
- Q #27) What are the methods for cross domain Ajax calls?
- Q #28) Describe Ajax Control Toolkit
- Q #29) List the protocols used by Ajax
- Q #30) When should we use Ajax GET request and Ajax POST request?
- Conclusion
Introduction To Ajax
Ajax is a technique to exchange data without page reload, that is a mix of tools like JavaScript, HTML, DHTML, XML, CSS, DOM, and Microsoft object – ActiveX. It uses XMLHttpRequest object for asynchronous data transfer, reducing bandwidth usage.
Various social media websites like Facebook, Instagram, Twitter use the Ajax technique to increase the count value when the user clicks the ‘Like’ button without refreshing the page.
Autosuggest is another feature that search engines like Google, Yahoo, or eCommerce sites like Amazon, BookMyShow, and Uber use. It is because of Ajax that fetches relevant data offering excellent options for users to choose from.
Most Popular Ajax Interview Questions
We have listed the most frequently asked Ajax Interview Questions with answers and explained the concept with examples below:
Q #1) How do Synchronous and Asynchronous Requests differ?
Answer: In a Synchronous request, the request sent from the client receives the response in the same HTTP connection. Examples being responses from Internet gateway, phone calls, and video meetings.
Whereas for Asynchronous requests, multiple requests can be sent from clients and their responses can be received in subsequent connections. Examples are collaborative documents like assessments, online queries, emails, and online forums.
Q #2) List the difference between JavaScript and Ajax
Answer: Ajax is an extension of JavaScript functionality.
The below table explains the differences between the two:
JavaScript | Ajax |
---|---|
It is open source scripting language for client side web development, even at server side (Node.js) | It is technology used for standalone and web based development, with libraries and framework that extends JS functionality |
JavaScript does not communicate/translate between programs developed in different languages. | It acts as translator that communicates among programs developed in different languages. |
JavaScript is client side scripting that are used in input validation, DOM handling, to add animated 2D/3D graphics on webpage and developing interactive Games | Ajax retrieves data from the server using XMLHttpRequest object. |
It is an add-on to HTML for functionalities like client side validations, DOM handling and event handling | Ajax send and receive data from server thereby updates portion of web page, without page reload |
Q #3) Describe Ajax.
Answer: Asynchronous JavaScript and XML (AJAX) is a technique that uses XMLHttpRequest objects to update web pages asynchronously by exchanging a small amount of data with the server, update the page without page reload. It is a front-end tool that communicates with the back-end server from the browser.
Q #4) List Ajax features.
Answer: Following are some of the important Ajax features:
- High performing web pages and user-friendly features like autocomplete suggestions
- Help in Template rendering at the client-side
- Client component declarative instantiation
- Use of Observer pattern on JavaScript arrays and objects
- Invoke ADO.Net data contexts and services
- Assists in Data View control and binding live data
Q #5) List advantages of using Ajax in web development
Answer: Following are few advantages of using Ajax in web development:
- Ajax reduces latency-response time in both Request/Response.
- XMLHttpRequest object in Ajax is highly responsive in data transfer in specific areas without page reload.
- Asynchronous calls to Server prevent client’s wait for data before rendering.
- Form validation is instant using Ajax.
- Bandwidth use is reduced when Ajax is used in fetching and storing data from the database in the background without page reload.
Q #6) What are the limitations of Ajax?
Answer: Limitations of Ajax are listed below:
- Ajax is not advisable in developing web applications but is great for websites.
- View page source display code is written in Ajax exposing the functionality.
- It is complex, less secured, needs more time in developing web pages.
- Search engines cannot index pages developed using Ajax as Crawlers cannot identify web applications developed in JavaScript and Ajax.
- XMLHttpRequest object can only fetch information from the server where pages are hosted but cannot fetch information from another server.
- It is not possible to bookmark pages developed in Ajax.
- Ajax cannot function with JavaScript disabled in the browser.
- It has a slow response time because different page controls load at different times.
- Dynamic page registration of its own on browser history engine is not possible, the code-behind file function does not work.
Q #7) List Ajax frameworks.
Answer: There are several Ajax frameworks developed in various languages.
These are:
- JavaScript frameworks like jQuery, MooTools, Prototype, YUI library, Dojo, AngularJS, Webix, GWT (Google Web Toolkit)
- Java frameworks like Apache Wicket, JSF (Java Server Faces), RichFaces, ICEfaces, PrimeFaces
- Windows .NET platform offers ASP.NET AJAX
- Python framework such as Pyjs.
- Ruby framework like Ruby on Rails
Q #8) Explain the working of Ajax.
Answer: Ajax with XMLHttpRequest object communicates with a server, renders data on a portion of a webpage without page reload.
List of steps that take place while working at Ajax are:
- The user request is sent to the server from the browser.
- JavaScript calls XMLHttpRequest object.
- The server interacts with the database using ASP.Net, JSP, or PHP.
- Data is fetched.
- XMLHttpRequest callback receives XML or JSON data from server.
- The browser displays HTML and CSS data on a particular portion of the page without page reload.
Q #9) What are the security threats that prevail with Ajax code?
Answer: The list of security threats while Ajax code is used in the webpage are listed below:
- Ajax request calls are in plain text format, resulting in insecure access to the database.
- Data retrieved gets stored on the client browser so anyone can view it.
- It makes monitoring browser sessions with help of a script.
- Entire Ajax code can be seen using view page source, hackers can misuse this code in a cyber attack.
Q #10) What is XMLHttpRequest?
Answer: XMLHttpRequest is an API that has methods and properties used by various scripting languages such as JavaScript, VBScript for manipulating and transferring XML data using HTTP protocol connecting client and server.
Various formats supported by XMLHttpRequest for data transfer are XML, JSON, plain text, and even binary content. XMLHttpRequest object help update parts of the webpage without reloading the page. It recognizes events that occur during the processing of the request.
Q #11) List various properties of XMLHttpRequest.
Answer: Following are lists of XMLHttpRequest properties:
- onreadystatechange – For every state changes event gets fired by event handler.
- readyState – Defines current state of XMLHttpRequest
- responseText – Response is returned as string.
- responseXML – Response is returned as XML in XML document object and parsed using DOM tree properties and methods.
- status – status is returned as number. Example: 404 for “Not Found” 200 for “OK”
- statusText – status is returned as text. Example: “Not Found” or “OK”
Q #12) Describe various methods used in XMLHttpRequest
Answer: The XMLHttpRequest methods are explained below:
- abort() – Used to cancel current request.
- getAllResponseHeaders() – Set of all the HTTP headers are returned as a string.
- getResponseHeader(header_name) – Returns specific HTTP header value.
- open(method,URL) – Various HTTP parameters such as GET, POST, HEAD, PUT and DELETE can be used as method. URL is the location of page on server where request should be sent.
- send(content) – Used to send the request to the server.
- setRequestHeader(label, value) – label-value pair is added to HTTP header to be sent.
Q #13) Explain various ways open() method can be used for XMLHttpRequest.
Answer: Open() method of XMLHttpRequest object help to initialize new request or existing request. The parameters it uses contains request method, URL and other optional attributes.
- open ( method, URL )
The value for the method parameter can be “GET”, “POST”, or “HEAD”. “PUT” and “DELETE” are some methods used in RESTful requests.
URL is the string that has the path of HTTP server path for sending a request. - open (method, URL, async)
async – specifies if the request send should be handled asynchronously or not at the server. If the value of async is true, it means that after send() method the script processing should not wait for a response, false will mean the script will wait for a response before continuing for script process. - open (method, URL, async, userName)
username is optional for authentication. Its default value is null. - open (method, URL, async, userName, password)
password is optional for authentication. Its default value is null.
Q #14) Explain Ajax callback function.
Answer: Following are steps carried out during Ajax callback:
- Client browser sends a request message to Server.
- The user is free to do anything other than waiting for the response as the request call is asynchronous in nature.
- The server receives the message and processes the page for which Ajax callback is called.
- The response is sent to the browser as a JavaScript code string that gets executed in the browser.
Q #15) List the steps to improve Ajax performance
Answer: In order to improve the performance of Ajax call, we can consider the following steps:
- Limiting Ajax requests to a minimum
- A wise selection of events that triggers Ajax request
- Optimum use of GET request
- Minimize data amount transmission
- Use Caching to secure data
Q #16) What are the ready state of requests used with Ajax requests?
Answer: The readyState is XMLHttpRequest property that describes the current state of XMLHttpRequest object.
State value | Explanation | State of XMLHttpRequest object |
---|---|---|
readyState = 0 | Request not initialized | XMLHttpRequest object is created but open() method not called |
readyState = 1 | Request is set | Open() method has been called, but before send() method is called |
readyState = 2 | Request is sent | After send() method is called |
readyState = 3 | Request is under process | Browser has connection established with Server, but Server has not yet completed with Response |
readyState = 4 | Request is complete | Request is completed, response data received at browser from the Server |
Q # 17) Describe events and their types in Ajax
Answer: When the user or browser interacts with HTML elements, it is termed an event.
Some examples of events are:
- onchange – When elements are changed like the text is changed or inserted into the text field.
- onclick – HTML elements like button, image, link are clicked.
- onload – Loading a page completed by the browser.
- onmouseover – When a user moves the mouse over HTML elements like image or text.
Ajax events are of two types: Local events and Global events
- Local events – Acts as callbacks inside Ajax request object
Examples: beforeSend, error, complete, success - Global events – triggered on document and calls listening event handlers
Examples: ajaxSend, ajaxSuccess, ajaxError, ajaxComplete
Q #18) List Ajax controls in ASP.Net
Answer: Following are the list of Ajax controls known as extensions found in Visual Studio IDE:
- Timer
- UpdatePanel
- UpdateProgress
- ScriptManager
- ScriptManagerProxy
Q #19) Explain UpdatePanel Control of ASP.Net
Answer: Update Panel is Ajax control in ASP.Net that refreshes a selected portion of a web page.
It is made up of two child tags:
- ContentTemplate
- Triggers
User control is placed in ContentTemplate tag, whereas certain triggers are defined in the Triggers tag that makes UpdatePanel control.update web page content.
The code in ASP that uses UpdatePanel control is below:
<asp:UpdatePanel ID="myupdatepane" runat="server">
The request is sent or data is posted to the server asynchronously without submitting the whole page.
Following is the ASPX page:
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Example to display UpdatePanel</title> <style type="text/css"> #updtpnl { width:450px; height:150px; } </style> </head> <body><form id="myform" runat="server"> <div style="padding-top: 10px"> <asp:ScriptManager ID="scrmnr" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="updtpnl" runat="server"> <ContentTemplate> <fieldset> <legend>UpdatePanel</legend> <asp:Label ID="lbl" runat="server" Text="Panel created."></asp:Label><br /> <asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="Button" /> </fieldset> </ContentTemplate> </asp:UpdatePanel><br /></div> </form></body></html>
Add the following code in C# page:
protected void Button1_Click(object sender, EventArgs e) { lbl.Text = "Refreshed at " + DateTime.Now.ToString(); }
The panel content should change every time button is clicked without page refresh.
Q #20) Describe ScriptManager in ASP.Net
Answer: ScriptManager in ASP.Net is central for Ajax functionality. It manages various Ajax resources on the page such as downloading the Microsoft Ajax Library and coordination of partial page upload with the help of UpdatePanel controls.
ASP code for Script Manager control:
<asp:ScriptManager ID=”scrm” runat=”server” ></asp:ScriptManager>
Q #21) What is the difference between ScriptManager and ScriptManagerProxy?
Answer: The difference between ScriptManager and ScriptManagerProxy is:
- If the page contains ScriptManager control, then ScriptManagerProxy enables nested components to add service reference and script.
- ScriptManager includes functionality that enables partial page rendering and Microsoft Ajax library.
Q #22) How to call Ajax from JavaScript?
Answer: There are many ways to call Ajax in JavaScript for submitting data to the server, checking username, creating a chat room, autocomplete form, vote or rate the product.
XMLHttpRequest object
Code example:
<script type="text/javascript"> function loadXMLDoc() { var myxmlhttp = new XMLHttpRequest(); myxmlhttp.onreadystatechange = function() { if (myxmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4 if (myxmlhttp.status == 200) { document.getElementById("myDiv").innerHTML = myxmlhttp.responseText; } else if (myxmlhttp.status == 400) { alert('There was an error 400'); } else { alert('something else other than 200 was returned'); } } }; myxmlhttp.open("GET", "ajax_info.txt", true); myxmlhttp.send(); } </script>
Fetch API
It is easy and flexible, supports modern browsers, follows a request-response model.
Code example:
fetch("myData/user_repo.json").then(function(response){ console.log(response); });
jQuery – It is a client-side JavaScript library
Code example:
$.ajax({ url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); } });
Q #23) List the tools to debug Ajax applications.
Answer: The list of tools used in debugging are:
- MyEclipse Ajax Tools
- Script Debugger
- JavaScript HTML Debugger
- Fiddler for IE (Internet Explorer)
- Firebug for Mozilla Firefox
Q #24) Describe the use of Cache false in Ajax call.
Answer: Ajax requests are always issued by default, but browsers may send responses from their cache. In order to prevent this, the cache is set to false. Cache: false can be applied to prevent caching of all future Ajax requests, irrespective of jQuery method used.
The code used for all Ajax functions is as below:
$.ajaxSetup({cache:false});
Q #25) How would you identify the completion of Ajax’s request?
Answer: In order to complete the Ajax request, the condition should be as below:
ReadyState property that defines current state of XMLHttpRequest if equals to four (xmlhttp.readyState == 4 && xmlhttp.status ==200) then data is available and Ajax request will be complete.
Code snippet:
xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { //Ajax request will be complete if above conditions are satisfied } }
Q #26) Explain the importance of client-side libraries
Answer: Client side libraries are similar to ASP.Net programming model,
- Handles browser compatibility issues by themselves.
- Make asynchronous request calls over XMLHTTP.
Q #27) What are the methods for cross domain Ajax calls?
Answer: There are two methods to transfer data between multiple security domains.
These are:
- CORS: Cross Origin Resource Sharing that works with HTTP web browsers
- JSONP: JSON with padding that works with legacy browsers and HTTP GET method.
Q #28) Describe Ajax Control Toolkit
Answer: Ajax Control Toolkit extends ASP.Net control functionalities. JavaScript code is used by Ajax Control Toolkit to enhance the capabilities for ASP.Net controls. ASP.Net Ajax control toolkit offers an infrastructure to write customizable, reusable, and extensible extenders and controls used to create an interactive web experience.
Ajax enabled ASP.Net tool kit can be added to your Visual Studio or Web Developer toolbox by drag-and-drop functionality.
Q #29) List the protocols used by Ajax
Answer: Following are the protocols that Ajax uses:
- JSON – For communication between client and server
- UED or URL encoded data
- HTTP’s GET or POST methods
- XMLHttpRequest that place a request with webserver
Q #30) When should we use Ajax GET request and Ajax POST request?
Answer: Use of HTTP GET or POST methods by Ajax depends on particular scenarios.
These are:
- When data for the given URL requested does not change, the HTTP GET request should be used by Ajax.
- HTTP POST is used for consistent web application architecture and for the updated state on the server.
Conclusion
Ajax is a technology to send or receive data asynchronously from client to server that returns data from the server without page reload.
Various technologies like DOM, JavaScript, ActiveX, XMLHttpRequest, HTML, XHTML and CSS are used by Ajax. Various protocols are used by various Ajax frameworks for quick data transfer without page reload.
We hope this guide on popular Ajax Interview Questions will help you prepare for your interview. We wish you all the best!