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
How to use the dojo/request:
- Load the
dojo/requestmodule using therequirefunction. - Call the
getorpostmethod to make a request.
Example
Let's make sample GET and POST requests using the dojo/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 therequirefunction to load thedojo/requestmodule. 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 definesuccessCallbackanderrorCallbackfunction. Invoke the get method of theDojoRequestobject withURL,successCallbackanderrorCallbackas arguments. Once the request succeeds thesuccessCallbackfunction will be invoked. If the request fails theerrorCallbackfunction will be invoked. - Lines 25–43: Create a function
sendPostRequest. Inside this method, we defined options for thePOSTrequest. In the options, we provided the data that is to be sent along with the request and headers for the request. Also, we createdsuccessCallbackanderrorCallbackfunctions. Invoke thepostmethod of theDojoRequestobject with (URL,options),successCallbackanderrorCallbackas arguments. Once the post request succeeds thesuccessCallbackfunction will be invoked. If the request fails theerrorCallbackfunction will be invoked.
Free Resources
Attributions:
- undefined by undefined
Copyright ©2025 Educative, Inc. All rights reserved