JavaScript AJAX

JavaScript Tutorial

JavaScript AJAX (Asynchronous JavaScript and XML)

In JavaScript AJAX allows web pages to update content without reloading the entire page.
It communicates with a server in the background and fetches data asynchronously.


Why Use AJAX?

FeatureBenefit
AsynchronousPage does not reload
FasterOnly required data loads
Used inAPIs, search suggestions, forms, dynamic content

Example: When searching in Google, results appear instantly — that’s AJAX.


 AJAX Uses in Real World

  • Auto-search suggestions

  • Loading data dynamically (products, users)

  • Submitting forms in the background

  • Fetching live data (weather, stock updates)


 AJAX Methods in JavaScript

MethodDescription
XMLHttpRequestOld traditional method
fetch()Modern and simpler
LibrariesjQuery AJAX (optional)


 AJAX with XMLHttpRequest (Old Method)


 


 AJAX with fetch() (Modern Way — Recommended)


Sending Data (POST Request)

Using Fetch


 AJAX Response Types

TypeExample
TextPlain text file
JSONMost common API response
XMLOld format
HTMLInsert into webpage section

 AJAX with DOM Example (Display Fetched Data)


 


 AJAX Error Handling


 AJAX + JSON + LocalStorage Example


 AJAX Keywords Summary

TermMeaning
AsyncRuns without blocking UI
RequestAsking server for data
ResponseServer sends data
APIService providing data
JSONFormat used to send data

 Summary

FeatureOldNew
SyntaxXMLHttpRequestfetch()
Easier? Hard Simple
Supports Promise?No Yes

You may also like...