Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
AJAX client
The AJAX client is a simple wrapper around
Axios.Default Axios Config
Some configuration is done by default. But it can be overruled.
timeout: 2500headers.common:Accept: application/jsonUsage
Is the AJAX Client is just an extended version of the Axios client all Axios
documentation is still valid. For the full documentatuon see
https://axios-http.com/docs/intro.
A simplified Stimulus controller that uses the AJAX Client:
Default toasts
Depending on HTTP status code and the provided data a toast will be shown. This
is done by using Interceptors. So
you can still use the promises of the Axios client.
Success (HTTP Status 2XX)
If the response object contains a
messagekey, a success toast will be shown.You can return a response like this:
The actual JSON will be:
{ "message": "The action was successful." }Error (HTTP Status != 2XX)
If the response object contains a
messagekey, an danger toast will be shown.If the message is not present the Exception message will be used.
or
Disable this behavior
You can disable the toast by passing a
disable_interceptor: falsein the response data.{ "message": "The action was successful", "disable_interceptor": true }CSRF token
A simple way to "protect" the AJAX calls is by using a CSRF token. This is done like below:
With this the csrf token is added to the payload of the request, with the key
csrf_token.In your controller you will need to check if the CSRF token is valid: