How can I make an AJAX call without jQuery?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You do not need jQuery to make AJAX requests in modern browsers. Native browser APIs already cover the common cases, and the best default today is fetch, with XMLHttpRequest kept around mainly for older codebases or specific compatibility requirements.
Use fetch for New Code
The fetch API is promise-based and works naturally with async and await. A simple GET request looks like this:
This is the modern replacement for a basic $.ajax call. The request is asynchronous, and the control flow is much easier to follow than old callback-heavy patterns.
Send JSON or Other Payloads Explicitly
For POST, PUT, or PATCH requests, specify the method, headers, and body clearly.
If the backend expects form data or URL-encoded payloads, send that format instead of assuming JSON is always correct.
Handle Errors Correctly
A common mistake is thinking fetch rejects the promise for HTTP errors such as 404 or 500. It does not. It only rejects for network-level failures, request abortion, or similar lower-level problems. You still need to inspect response.ok.
This keeps HTTP status handling explicit instead of assuming the runtime will treat all failure modes the same way.
XMLHttpRequest Still Exists
If you maintain older code, XMLHttpRequest is still a valid browser API. It is more verbose, but it works without jQuery too.
The main reason to prefer fetch in new code is readability and simpler composition with async JavaScript.
Connect the Request to the UI
In real applications, a request is only part of the job. The UI should show loading state, success, and failure clearly.
That turns the request into a real interaction instead of just a console demo.
Common Pitfalls
- Expecting
fetchto throw automatically for404or500responses leads to incomplete error handling. - Sending JSON when the server expects a different content type causes avoidable API bugs.
- Updating the UI before checking
response.okand parsing the response safely makes failures confusing. - Keeping jQuery only for AJAX in a modern browser-only app adds dependency weight without much value.
- Ignoring cancellation or repeated requests can create race conditions in interactive screens.
Summary
- Use
fetchas the default way to make AJAX requests without jQuery. - Specify method, headers, and body explicitly for non-
GETrequests. - Check
response.okbecause HTTP errors do not rejectfetchautomatically. - Use
XMLHttpRequestonly for legacy or compatibility-driven code. - Connect request state to the UI so loading and failure are visible to the user.
Related reading
- How can I make Bootstrap columns all the same height?
- How can I make the console.log at the bottom wait until its all complete, then show me the answer instead of waiting?
- How can I map a DynamoDB AttributeMap type to an interface?
- How can I pass a parameter to a setTimeout() callback?
- How can I position my div at the bottom of its container?
- How can I properly write this shader function in JS?
- How can I regenerate ios folder in React Native project?
- How can I remove a style added with .css() function?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.