The Ultimate AJAX Requests Cheat Sheet

Introduction to AJAX

AJAX (Asynchronous JavaScript and XML) enables web applications to send and receive data from a server asynchronously without refreshing the entire page. This creates faster, more responsive web applications by updating specific parts of a page without disrupting the user experience. Despite the name, modern AJAX commonly uses JSON rather than XML for data formatting.

Core Concepts of AJAX

ConceptDescription
AsynchronousOperations occur independently of the main program flow, allowing other code to execute while waiting for server responses
Client-Server CommunicationBrowser (client) sends requests to server and processes responses without page reloads
XMLHttpRequest/FetchJavaScript objects used to make HTTP requests to servers
DOM ManipulationUsing JavaScript to update specific parts of the page with new data
Data FormatsCommonly JSON, but can also be XML, HTML, or plain text

AJAX Request Methods

XMLHttpRequest (Traditional Approach)

 
javascript
// Create XMLHttpRequest object
const xhr = new XMLHttpRequest();

// Configure the request
xhr.open('GET', 'https://api.example.com/data', true);

// Set up response handler
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      const response = JSON.parse(xhr.responseText);
      console.log(response);
    } else {
      console.error('Request failed with status:', xhr.status);
    }
  }
};

// Send the request
xhr.send();

Fetch API (Modern Approach)

 
javascript
fetch('https://api.example.com/data')
  .then(response => {
    if (!response.ok) {
      throw new Error('Network response was not ok: ' + response.status);
    }
    return response.json();
  })
  .then(data => {
    console.log(data);
  })
  .catch(error => {
    console.error('Fetch error:', error);
  });

Axios (Popular Library)

 
javascript
// GET request
axios.get('https://api.example.com/data')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

// POST request
axios.post('https://api.example.com/create', {
  name: 'John',
  email: 'john@example.com'
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

jQuery AJAX (Legacy)

 
javascript
$.ajax({
  url: 'https://api.example.com/data',
  method: 'GET',
  dataType: 'json',
  success: function(data) {
    console.log(data);
  },
  error: function(xhr, status, error) {
    console.error('Error:', error);
  }
});

HTTP Methods for AJAX Requests

MethodPurposeExample Use Case
GETRetrieve dataFetching user profiles, search results
POSTCreate new resourcesSubmitting forms, creating new accounts
PUTUpdate entire resourcesUpdating complete user profiles
PATCHUpdate partial resourcesUpdating specific fields (e.g., email only)
DELETERemove resourcesDeleting user accounts or posts

Working with Different Data Formats

JSON

 
javascript
// Parsing JSON
const jsonString = '{"name": "John", "age": 30}';
const data = JSON.parse(jsonString);

// Converting to JSON
const user = { name: 'John', age: 30 };
const jsonString = JSON.stringify(user);

FormData (for forms and file uploads)

 
javascript
const formData = new FormData();
formData.append('username', 'john');
formData.append('avatar', fileInput.files[0]);

fetch('https://api.example.com/upload', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.then(data => console.log(data));

Handling AJAX Responses

Status Codes to Handle

Code RangeMeaningCommon Examples
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved Permanently, 304 Not Modified
4xxClient Error400 Bad Request, 401 Unauthorized, 404 Not Found
5xxServer Error500 Internal Server Error, 503 Service Unavailable

Response Processing

 
javascript
fetch('https://api.example.com/data')
  .then(response => {
    // Check status
    if (!response.ok) {
      if (response.status === 404) {
        throw new Error('Resource not found');
      } else if (response.status === 401) {
        throw new Error('Authentication required');
      } else {
        throw new Error('Network response was not ok');
      }
    }
    
    // Check content type and process accordingly
    const contentType = response.headers.get('content-type');
    if (contentType && contentType.includes('application/json')) {
      return response.json();
    } else if (contentType && contentType.includes('text/html')) {
      return response.text();
    } else {
      throw new Error('Unsupported content type');
    }
  })
  .then(data => {
    // Process the data
    console.log(data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Async/Await with AJAX

 
javascript
async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    
    const data = await response.json();
    console.log(data);
    return data;
  } catch (error) {
    console.error('Fetch error:', error);
  }
}

// Call the async function
fetchData();

Common AJAX Challenges and Solutions

ChallengeSolution
CORS IssuesUse proper headers, implement CORS on server, or use a proxy
Handling TimeoutsImplement timeout settings and provide user feedback
Managing Loading StatesUse loading indicators/spinners during requests
Error HandlingImplement comprehensive try/catch blocks and user-friendly error messages
Sequential RequestsUse Promise chaining or async/await for dependent requests
Parallel RequestsUse Promise.all() to manage multiple simultaneous requests
AuthenticationInclude auth tokens in request headers
CachingImplement client-side caching of responses when appropriate

Best Practices for AJAX Requests

  • Always validate user inputs before sending to server
  • Use appropriate HTTP methods for different operations
  • Include error handling for both network and server errors
  • Show loading indicators for operations that might take time
  • Implement request cancellation for long-running or unnecessary requests
  • Rate limit requests to prevent overwhelming servers or triggering API limits
  • Use HTTPS for all requests to ensure data security
  • Cache responses when appropriate to reduce server load and improve performance
  • Add CSRF tokens to requests when needed for security
  • Implement retry logic for intermittent failures

Cross-Browser Compatibility

 
javascript
// Cross-browser XMLHttpRequest
function createXHR() {
  if (typeof XMLHttpRequest !== 'undefined') {
    return new XMLHttpRequest();
  } else {
    try {
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        throw new Error("XMLHttpRequest is not supported");
      }
    }
  }
}

Modern AJAX Techniques

Fetch with AbortController (Request Cancellation)

 
javascript
const controller = new AbortController();
const signal = controller.signal;

// Set a timeout to abort after 5 seconds
setTimeout(() => controller.abort(), 5000);

fetch('https://api.example.com/data', { signal })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => {
    if (error.name === 'AbortError') {
      console.log('Request was aborted due to timeout');
    } else {
      console.error('Error:', error);
    }
  });

Service Workers for Offline AJAX

 
javascript
// In your service worker file
self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(cachedResponse => {
        if (cachedResponse) {
          return cachedResponse;
        }
        return fetch(event.request)
          .then(response => {
            // Cache the response for future offline use
            return caches.open('v1').then(cache => {
              cache.put(event.request, response.clone());
              return response;
            });
          });
      })
  );
});

Resources for Further Learning

Scroll to Top