AJAX (Asynchronous JavaScript and XML) Cheat Sheet:
- AJAX Basics:
- AJAX allows you to update parts of a web page without reloading the entire page.
- It uses JavaScript to send and receive data from a server asynchronously.
- AJAX can be used to retrieve data from a server, send data to a server, and update parts of a web page dynamically.
- XMLHttpRequest Object:
- The XMLHttpRequest object is the core of AJAX.
- It allows you to send HTTP requests to a server and handle the response.
- Create a new XMLHttpRequest object:
var xhttp = new XMLHttpRequest();
- AJAX Workflow:
- Create an XMLHttpRequest object.
- Define a callback function to handle the server response.
- Open a connection to the server using the
open()
method. - Send the request to the server using the
send()
method. - Handle the server response in the callback function.
- AJAX Methods:
open(method, url, async)
: Specifies the type of request, URL, and whether the request should be asynchronous.send(data)
: Sends the request to the server. Optional data can be included.abort()
: Cancels the current request.setRequestHeader(header, value)
: Sets the value of an HTTP request header.
- AJAX Events:
onreadystatechange
: Triggered whenever thereadyState
property changes.onload
: Triggered when the request has been completed successfully.onerror
: Triggered if an error occurs during the request.
- AJAX Ready States:
0
: Request not initialized.1
: Server connection established.2
: Request received.3
: Processing request.4
: Request finished and response is ready.
- AJAX Response Handling:
- Use the
responseText
property to get the response as a string. - Use the
responseXML
property to get the response as an XML document. - Use the
status
property to get the HTTP status code of the response. - Use the
statusText
property to get the status text of the response.
- AJAX Cross-Origin Requests:
- AJAX requests are subject to the same-origin policy, which restricts requests to the same domain.
- To make cross-origin requests, the server must include the appropriate CORS (Cross-Origin Resource Sharing) headers.
- Alternatively, you can use JSONP (JSON with Padding) or a proxy server to overcome cross-origin restrictions.
- AJAX Libraries/Frameworks:
- jQuery: A popular JavaScript library that simplifies AJAX requests with its
$.ajax()
method. - Axios: A lightweight HTTP client library that supports AJAX requests in both browsers and Node.js.
- Fetch API: A modern browser API for making AJAX requests, providing a more powerful and flexible alternative to XMLHttpRequest.
Remember to always handle errors, sanitize user input, and secure your AJAX requests to prevent security vulnerabilities.