Ajax
Ajax
Ajax
Source : https://www.tutorialspoint.com/ajax/index.htm
What is AJAX?
AJAX is a new technique for creating better, faster, and more interactive web applications with the help of
XML, HTML, CSS, and Java Script
Ajax uses CSS for presentation, along with Document Object Model and JavaScript for dynamic content
display.
Conventional web applications transmit information to and from the sever using synchronous requests. It
means you fill out a form, hit submit, and get directed to a new page with new information from the server.
With AJAX, when you hit submit, JavaScript will make a request to the server, interpret the results, and
update the current screen. In the purest sense, the user would never know that anything was even transmitted
to the server.
AJAX is a web browser technology independent of web server software
A user can continue to use the application while the client program requests information from the server in
the background.
Intuitive and natural user interaction. Clicking is not required, mouse movement is a sufficient event trigger.
What is AJAX?
AJAX applications are much more faster and responsive as compared to traditional web applications.
It creates a great balance between the client and the server by allowing them to communicate in the
background while the user is working in the foreground.
In the AJAX applications, the exchange of data between a web browser and the server is asynchronous
means AJAX applications submit requests to the web server without pausing the execution of the application
and can also process the requested data whenever it is returned.
For example, Facebook uses the AJAX model so whenever we like any post the count of the like button
increase instead of refreshing the whole page.
Working of AJAX.
Traditional web applications are created by adding loosely web pages through links in a predefined order.
Where the user can move from one page to another page to interact with the different portions of the
applications.
Also, HTTP requests are used to submit the web server in response to the user action.
After receiving the request the web server fulfills the request by returning a new webpage which, then
displays on the web browser.
This process includes lots of pages refreshing and waiting
AJAX change this whole working model by sharing the minimum amount of data between the web browser
and server asynchronously.
Working of AJAX. (continued)
It creates an additional layer known as AJAX engine in between the web application and web server due to
which we can make background server calls using JavaScript and retrieve the required data, can update the
requested portion of a web page without casing full reload of the page.
It reduces the page refresh timing and provides a fast and responsive experience to the user.
Asynchronous processes reduce the workload of the web server by dividing the work with the client
computer.
Due to the reduced workload web servers become more responsive and fast.
Advantages of AJAX
AJAX is fully dependent on JavaScript. So if anything happens with JavaScript in the browser AJAX will
not support.
The debugging of AJAX applications is difficult.
If one request can fail then it can fail the load of the whole webpage.
If JavaScript is disabled in your web browser then you are not able to run the AJAX webpage
Dynamic Versus Static Website
It is a combination of web technologies that allows to establish asynchronous communication between the
web server and the web browser.
It creates a dynamic application that updates the content of the webpage dynamically without reloading the
whole page.
AJAX is not a programming language or script language, but it combines multiple web-related technologies
like HTML, XHTML, CSS, JavaScript, DOM, XML, XSLT and XMLHttpRequest object.
1. Javascript
Javascript − It is an important part of AJAX. It allows you to create client-side functionality. Or we can say
that it is used to create AJAX applications.
2. XML or JSON
XML or JSON − XML stands for extensible markup language whereas JSON stands for JavaScript Object
Notation. Both JSON and XML are used on the client side to exchange data between the web server and the
client.
3. XMLHttpRequest
The XMLHttpRequest − It is used to perform asynchronous data exchange between a web browser and a
web server. It is a JavaScript object that performs asynchronous operations
Where using a new keyword along with XMLHttpRequest() constructor we can be able to create a new
XMLHttpRequest object.
This object must be created before calling the open() function to initialise it before calling send() function to
send the request to the web server.
4. HTML and CSS
HTML and CSS − It is used to provide markup and style to the webpage text.
5. DOM
DOM − It is used to interact with and alter the webpage layout and content dynamically.
Or we can say that DOM is used to create a logical representation of the elements that are used to markup
HTML pages
It is not a part of JavaScript, but using javaScript we can access the methods and the properties of DOM
objects.
Using DOM methods and properties we can create or modify HTML pages.
AJAX - Action
This part of AJAX-ACTION gives you a clear picture of the exact steps of AJAX operation.
Steps of AJAX Operation
1. A client event occurs.
2. An XMLHttpRequest object is created.
3. The XMLHttpRequest object is configured.
4. The XMLHttpRequest object makes an asynchronous request to the Webserver.
5. The Webserver returns the result containing XML or JSON document.
6. The XMLHttpRequest object calls the callback() function and processes the result.
7. The HTML DOM is updated.
AJAX - XMLHttpRequest
5. send(): It is used to send requests to the web server. It is generally used for GET requests.
6. send(string): It is used to send requests to the server. It is generally used for POST requests.
7. setRequestHeader(): It is used to add key/value pair to the header
AJAX - Database Operations
AJAX is the most commonly used web technique to send and receive data to and from the web server
asynchronously without disturbing the functionality of the other components of the client-side application.
Although AJAX itself does not provide any security vulnerabilities, still we have to keep some security
measurements while implementing AJAX
Security measurements set to follow
1. Cross-Site Scripting(XSS)
In this attack, the attacker tricks the browser by doing unwanted actions with the help of an authentication
session.
It can exploit the AJAX request and can perform unauthorized actions.
So to prevent this attack we have to implement CSRF protection techniques like generation and validating
random tokens Or can use the same origin policy.
3. Insecure Direct Object References(IDOR)
The request generally accesses the specified resource from the server with the help of a unique identifier.
But if the attacker gets this identifier then it can easily manipulate or can access unauthorized resources.
So to prevent this avoid exposing sensitive information.
Also, check the user authorization for the specified resource of the developers, in the server side.
4. Content Security Policies(CSP)
It is a policy which helps users/developers to save themselves from malicious activities or unauthorized
access.
It provides a permitted source for secure scripts and other resources.
5. Server-Side validation
Server-side validation is very important because it ensures that the submitted data meets the specified criteria
and it is safe for further process.
We can not bypass or manipulate server-side validation but we can bypass client-side validation.
6. Secure Session Management
The AJAX application should properly maintain user sessions and session tokens to save the session from
attacks.
Always check that the session tokes are generated properly, and securely transmitted and can logout if the
invalidation or session expiration happens
7. Input Validation and Sanitization
Server should perform validation and sanitization of the data received from the client side to prevent attacks.
8. Regular Update and Security
Every technology in this world has its bright side and its dark side similarly AJAX is a powerful technique
which is used to develop dynamic and interactive web applications but it also has some challenges and issues
1. Cross-Domain Requests
In AJAX, the requests can be attacked using XSS(cross-site scripting) or CSRF(cross-site request forgery).
So to avoid such types of vulnerabilities we have to use input validation, output encoding, and CSRF protect
tokens
3. Performance Impact
If we do not optimize the AJAX request properly then it will affect the performance.
If we transfer excessive data, unnecessary requests, frequent requests, or inefficient server-side processing
these activities are responsible for slowing down the page loading time and can increase the load of the
server.
So always make a proper and optimized request.
4. Search Engine Optimization(SEO)
Search engines often face challenges in indexing AJAX-driven content because old web crawlers do not
execute JavaScript.
It will affect the ranking and discovery of the web page in the search engines
5. Testing and Debugging
Due to the asynchronous behaviour of the request it is hard to debug AJAX codes.
So to overcome this issue we have to use good debugging tools that can identify the issues and resolve them
correctly
6. JavaScript Dependency
Both JSON and XML can be used to receive data from a web server. The following JSON and XML
examples both define an employees object, with an array of 3 employees:
JSON vs XML (continued)
JSON XML
<employees>
<employee>
<firstName>John</
{"employees":[ firstName> <lastName>Doe</lastName>
{ "firstName":"John", "lastName": </employee>
"Doe" }, <employee>
{ "firstName":"Anna", "lastName" <firstName>Anna</
:"Smith" }, firstName> <lastName>Smith</lastName>
{ "firstName":"Peter", "lastName": </employee>
"Jones" } <employee>
]} <firstName>Peter</
firstName> <lastName>Jones</lastName>
</employee>
</employees>
JSON is Like XML Because
In JSON, objects refer to dictionaries, which are enclosed in curly brackets, i.e., { }.
These objects are written in key/value pairs, where the key has to be a string and values have to be a valid
JSON data type such as string, number, object, Boolean or null.
Here the key and values are separated by a colon, and a comma separates each key/value pair.
JSON Data Types
String A string is always written in double-quotes. It may consist of numbers, "student", "name", "1234", "Ver_1"
alphanumeric and special characters.
Lightweight: When used over the network, the JSON keeps the overhead involved in processing extra
header data to a minimum.
Traversable: It is designed to traverse easily across the network. This helps in its role as the primary data
representation for MongoDB.
Efficient: It allows C data types that allow easy and quick encoding and decoding of data.
Differences between JSON and BSON
JSON BSON
JSON contains some basic data types like string, numbers, Boolean, null. BSON contains some additional data types like date, timestamp, etc.
Databases like AnyDB, redis, etc. stores the data into the JSON format. The data in MongoDB is stored in a BSON format.
JSON requires less space as compared to BSON. BSON requires more space as compared to JSON.
It is used for the transmission of data. It is used for the storage of data.
It does not have encoding and decoding technique. It has faster encoding and decoding technique.