Fetch Library (Ajax Requests)
The Stratox framework provides built-in Ajax integration using the JavaScript Fetch API, which you can utilize if needed. While it's not mandatory, you can easily integrate your own Ajax library in the dispatcher within the main JavaScript file, main.js
.
The Ajax response will be passed to the controllers!
Triggering Ajax Calls
You can trigger Ajax calls in 3 different ways:
Bind Ajax call to a view and the view's response
Configure the main app's settings to enable Ajax calls for every router request automatically (though you can manually disable Ajax calls in each route if necessary).
Manually add Ajax configuration in each route.
Or all of the above
1. Binding Ajax to a View
Binding an Ajax request to a view allows the view to automatically trigger the request upon rendering, updating with the JSON response as soon as it's received.
Quick Start
Here’s the fastest way to bind an Ajax request to a view:
That’s it! The view will display the response from the URL as soon as the data is fetched.
You can make GET, POST, PUT, and DELETE requests by simply changing the get
method above to the appropriate request type.
url: Expects a string and URL value
config: expects a object and is the Fetch API config
data: expects a object and it is the the parsed body/post request items
Modifying the Response
You can modify the Ajax response before passing it to the view. Here’s how:
You can make GET, POST, PUT, and DELETE requests by simply specifying it in the setMethod
method to the appropriate request type:
If you want to configure the fetch API, you pass an object in second parameter of the class:
Creating the View
The view function will receive the Ajax response data once the request is complete. While waiting for the data, you can use context.isLoading()
to manage loading states and display an appropriate message if needed.
Here’s an example:
Summary
Quick Setup: Simply pass the Ajax request to
this.layout
.Modify Data: Use
.execute()
to adjust the response before it reaches the view.Loading State: Use
context.isLoading()
to manage loading behavior and provide feedback to the user while waiting for the request to complete.
This approach allows for flexible and dynamic updates within your views, with minimal setup.
2. Enable Ajax on Every Router Request
To enable Ajax for every router request, open the main JavaScript file ./src/main.js
and add the request
configuration to the App
class. Below, I've filled in each config option:
Note: The full configuration list is at the end of the guide.
Click here for full configuration.
The dataType
configuration automatically sets certain headers in the fetch config to correspond to each data type. However, you can overwrite these headers with your own custom headers if needed.
3. Enable Ajax Manually on a Router
To enable Ajax manually on a specific router, open the main JavaScript file ./src/routes/app.js
and add the request
configuration to the route. Below, I've filled in each config option:
Click here for full configuration.
With these configurations, you can easily enable Ajax functionality for your Stratox application, either globally or on specific routes. Adjust the options as needed to suit your application's requirements.
Accessing the Ajax Response
Accessing the Ajax response is as simple as accessing the http
parameter to retrieve the Ajax request body response. This process works the same in controllers.
In this example, http.response
allows you to access the Ajax response data within the router/controller function. You can then use this data as needed in your application logic.
Fetch Ajax Configurations
For the fetch option 2-3, both approaches use the same object parameters:
url: The URL to your site/API location to access via Ajax.
dataType: Set expected response data type (
json
,xml
, ortext
). JSON response will be converted to an object, XML toDOMParser
, and text will be returned as is. (default:json
)path: Overwrites the expected URI path. (optional)
startPath: Adds a default start path if the URI path is empty. (optional)
config: Supplies the Fetch API with options. You can read more about these options here. (optional)
get: Passes or modifies a GET parameter for the controller result. (optional)
post: Passes or modifies a POST parameter for the controller result. (optional)
Last updated