What is AJAX with the dojo/request function?

What is Dojo?

The Dojo is a JavaScript toolkit(library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more. Read more about Dojo at their homepage.

What is AJAX with dojo/request?

Using dojo/request API, we can make AJAX requestssend request and receive response without reloading the page to a server from the client asynchronously. This module simplifies how we can make an AJAX request to the server.

How to use the dojo/request:

  1. Load the dojo/request module using the require function.
  2. Call the get or post method to make a request.

Example

Let's make sample GET and POST requests using the dojo/request.

Console
Using request/dojo to make an AJAX request

Explanation

In the above code snippet:

  • Line 6: Load the Dojo library from the CDN server.
  • Lines 11–15: Create a function loadDojo. This function will use the require function to load the dojo/request module. Once that module is loaded we assign the request object to a variable. So that we can use that to send AJAX requests.
  • Lines 17–23: Create a function sendGetRequest. Inside this method we define successCallback and errorCallback function. Invoke the get method of the DojoRequest object with URL, successCallback and errorCallback as arguments. Once the request succeeds the successCallback function will be invoked. If the request fails the errorCallback function will be invoked.
  • Lines 25–43: Create a function sendPostRequest. Inside this method, we defined options for the POST request. In the options, we provided the data that is to be sent along with the request and headers for the request. Also, we created successCallback and errorCallback functions. Invoke the post method of the DojoRequest object with (URL,options), successCallback and errorCallback as arguments. Once the post request succeeds the successCallback function will be invoked. If the request fails the errorCallback function will be invoked.

Free Resources

Attributions:
  1. undefined by undefined
Copyright ©2025 Educative, Inc. All rights reserved