Skip to content

Latest commit

 

History

History
135 lines (106 loc) · 5.02 KB

jQuery AJAX.md

File metadata and controls

135 lines (106 loc) · 5.02 KB

jQuery AJAX: Comprehensive Guide

This documentation covers practical examples of using jQuery's $.ajax method for handling asynchronous HTTP requests. It includes examples for standard AJAX requests and handling multimedia uploads with progress tracking.


Table of Contents

  1. Introduction
  2. Basic AJAX Request
  3. AJAX Multimedia Request with Progress Tracking
  4. Best Practices

Introduction

jQuery's $.ajax is a powerful method for sending and receiving HTTP requests asynchronously. It allows you to communicate with the server without requiring a page reload, making your applications more dynamic and responsive.


Basic AJAX Request

This example demonstrates a simple POST request to a PHP endpoint.

Code Example

$.ajax({
  url: "page.php", // Target URL
  type: "POST", // Request method
  data: { "username": "username_value" }, // Data to send
  dataType: "json", // Expected data type of response
  success: function(response, status, xhr) {
    console.log(response); // Handle success
  },
  error: function(xhr, status, err) {
    console.log(xhr); // Handle errors
  },
  complete: function(xhr, status) {
    console.log(xhr); // Always executed after success or error
  }
});

Explanation

  • url: Specifies the endpoint URL to which the request is sent.
  • type: HTTP request method (e.g., GET, POST, PUT, DELETE).
  • data: Data sent to the server (as an object or a serialized string).
  • dataType: The expected response format (json, html, text, etc.).
  • Callback Functions:
    • success: Invoked if the request is successful.
    • error: Invoked if the request fails.
    • complete: Invoked after both success and error.

AJAX Multimedia Request with Progress Tracking

This example shows how to upload multimedia files and track the upload progress.

Code Example

$.ajax({
  url: "page.php", // Target URL
  enctype: "multipart/form-data", // Encoding type for file uploads
  type: "POST", // Request method
  dataType: "json", // Expected data type of response
  data: new FormData($("#my_form")[0]), // Form data for the request
  processData: false, // Prevents jQuery from processing data
  contentType: false, // Prevents jQuery from setting content-type header
  beforeSend: function() {
    $("#my_form").trigger("reset"); // Resets the form before sending
  },
  xhr: function() {
    var xhr = $.ajaxSettings.xhr(); // Gets native XMLHttpRequest object
    xhr.upload.onprogress = function(data) {
      console.log(data.loaded, data.total); // Logs upload progress
      // Example of calculating percentage:
      // var percent = Math.round((data.loaded / data.total) * 100);
      // console.log(percent + "% completed");
    };
    return xhr;
  },
  success: function(response, status, xhr) {
    console.log(response); // Handle success
  },
  error: function(xhr, status, err) {
    console.log(xhr); // Handle errors
  },
  complete: function(xhr, status) {
    console.log(xhr); // Always executed after success or error
  }
});

Explanation

  • enctype: Specifies multipart/form-data for file uploads.
  • processData: Ensures the data is sent as FormData without being transformed.
  • contentType: Disables automatic Content-Type header creation.
  • xhr: Customizes the request to track upload progress.
    • onprogress: Tracks the upload progress by monitoring loaded and total data.

Best Practices

  1. Validate Inputs: Always validate the data on both client and server sides before sending or processing requests.

  2. Error Handling:

    • Implement detailed error logging in the error callback.
    • Display user-friendly messages for common errors (e.g., "Network error, please try again").
  3. Security:

    • Use HTTPS to secure data transmission.
    • Sanitize and validate all server-side data to prevent injection attacks.
  4. Asynchronous Design:

    • Use the complete callback for tasks that should execute regardless of success or failure.
    • Avoid blocking operations; let the user interact with the application while requests are in progress.
  5. File Uploads:

    • Monitor upload progress to improve user experience.
    • Set reasonable file size and type restrictions on the server.
  6. Browser Compatibility:

    • Ensure that the AJAX methods and FormData API are supported in your target browsers.

Conclusion

This guide provides a practical approach to using jQuery's $.ajax method for handling HTTP requests and managing file uploads with progress tracking. With these examples, you can build dynamic, responsive web applications that provide a seamless user experience.

If you encounter any issues or have suggestions, feel free to contribute!