You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.md
+36-4Lines changed: 36 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ Larajax includes a small client-side layer that lets you call your server-side h
124
124
125
125
When a request is made, it is sent to the **current page URL** and the target handler is passed in the `X-AJAX-HANDLER` request header. This keeps all requests scoped to the active page route while avoiding the need for exposed API endpoints.
126
126
127
-
### Markup Tools
127
+
### From Markup
128
128
129
129
The `data-request` attribute is the primary way to trigger an AJAX handler from HTML.
130
130
@@ -150,7 +150,7 @@ When a request is triggered from inside a form, the form inputs are automaticall
150
150
For a full list of supported attributes and behaviors, see the [AJAX Handlers Guide](./guide/ajax-handlers.md).
151
151
:::
152
152
153
-
### JavaScript Tools
153
+
### From JavaScript
154
154
155
155
When markup alone is not enough, the same handlers can be called directly from JavaScript using `jax.ajax()`.
156
156
@@ -174,7 +174,39 @@ When you want to serialize the input values of a container or form explicitly, y
174
174
175
175
This gives you the same request model as data-request, but with full control over when and how the call is made.
176
176
177
-
178
177
::: tip
179
178
More details on working with JavaScript are available in the [JavaScript Guide](./guide/ajax-javascript.md).
180
-
:::
179
+
:::
180
+
181
+
### From the Controller
182
+
183
+
Larajax provides a global `ajax()` helper that returns an instance of the `Larajax\Classes\AjaxResponse` class. This response object lets you compose multiple instructions into a single server response and describe exactly how the browser should react.
184
+
185
+
Instead of returning raw JSON and handling everything manually on the client, you describe the outcome of the action directly in the controller.
186
+
187
+
```php
188
+
function onSave()
189
+
{
190
+
return ajax()
191
+
// Include some response data
192
+
->data(['success' => true])
193
+
194
+
// Patch a DOM element
195
+
->update(['#someElement' => 'Hello world!'])
196
+
197
+
// Load a JavaScript file
198
+
->js('assets/js/app.js')
199
+
200
+
// Trigger a halting browser event
201
+
->browserEventAsync('app:redirecting')
202
+
203
+
// Redirect the browser
204
+
->redirect('https://larajax.org');
205
+
}
206
+
```
207
+
208
+
Each call adds another instruction to the response queue. When the request completes, the client-side runtime processes these instructions in order and applies the corresponding changes to the page. This keeps your controller in full control of both the server-side logic and the resulting UI behavior.
209
+
210
+
::: tip
211
+
To explore the full set of available response operations, see the [AJAX Response Guide](./guide/ajax-responses.md).
0 commit comments