Skip to content

Commit 5925c3a

Browse files
authored
docs: refactor AJAX request and clarify framework examples (codeigniter4#10129)
1 parent e66d025 commit 5925c3a

1 file changed

Lines changed: 42 additions & 18 deletions

File tree

user_guide_src/source/general/ajax.rst

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
AJAX Requests
33
##############
44

5-
The ``IncomingRequest::isAJAX()`` method uses the ``X-Requested-With`` header to define whether the request is XHR or normal. However, the most recent JavaScript implementations (i.e., fetch) no longer send this header along with the request, thus the use of ``IncomingRequest::isAJAX()`` becomes less reliable, because without this header it is not possible to define whether the request is or not XHR.
5+
The ``IncomingRequest::isAJAX()`` method uses the ``X-Requested-With`` header to define whether the request is XHR or normal. However, modern JavaScript APIs such as ``fetch`` no longer send this header by default, so ``IncomingRequest::isAJAX()`` becomes less reliable without additional configuration.
66

7-
To get around this problem, the most efficient solution (so far) is to manually define the request header, forcing the information to be sent to the server, which will then be able to identify that the request is XHR.
7+
To work around this problem, manually define the request header so the server can identify the request as XHR.
88

9-
Here's how to force the ``X-Requested-With`` header to be sent in the Fetch API and other JavaScript libraries.
9+
Here are common ways to send the ``X-Requested-With`` header in the Fetch API and other JavaScript libraries.
1010

1111
.. contents::
1212
:local:
@@ -28,41 +28,54 @@ Fetch API
2828
Axios
2929
=====
3030

31-
If you are using Axios, it also does not include the ``X-Requested-With`` header by default.
31+
Axios does not include the ``X-Requested-With`` header by default.
3232
You can add it globally as follows:
3333

3434
.. code-block:: javascript
3535
3636
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
3737
3838
39-
jQuery
40-
======
41-
42-
For libraries like jQuery for example, it is not necessary to make explicit the sending of this header, because according to the `official documentation <https://api.jquery.com/jquery.ajax/>`_ it is a standard header for all requests ``$.ajax()``. But if you still want to force the shipment to not take risks, just do it as follows:
39+
If you prefer to avoid global defaults, create an Axios instance instead:
4340

4441
.. code-block:: javascript
4542
46-
$.ajax({
47-
url: "your url",
48-
headers: {'X-Requested-With': 'XMLHttpRequest'}
43+
const api = axios.create({
44+
headers: {
45+
'X-Requested-With': 'XMLHttpRequest'
46+
}
4947
});
5048
51-
VueJS
52-
=====
5349
54-
In VueJS you just need to add the following code to the ``created`` function, as long as you are using Axios for this type of request.
50+
Vue.js
51+
-------
52+
53+
Vue does not require a specific HTTP client. If your Vue app uses Axios, configure Axios once during application bootstrap or in a shared API module, and reuse that configuration throughout the app.
54+
55+
React
56+
-----
57+
58+
React also does not provide a built-in HTTP client. If your React app uses Axios, reuse the shared Axios configuration above, or set the header for an individual request when needed:
5559

5660
.. code-block:: javascript
5761
58-
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
62+
axios.get('your url', {
63+
headers: {
64+
'X-Requested-With': 'XMLHttpRequest'
65+
}
66+
})
5967
60-
React
61-
=====
68+
jQuery
69+
======
70+
71+
For libraries like jQuery for example, it is not necessary to make explicit the sending of this header, because according to the `official documentation <https://api.jquery.com/jquery.ajax/>`_ it is a standard header for all requests ``$.ajax()``. But if you still want to force the shipment to not take risks, just do it as follows:
6272

6373
.. code-block:: javascript
6474
65-
axios.get("your url", {headers: {'Content-Type': 'application/json'}})
75+
$.ajax({
76+
url: "your url",
77+
headers: {'X-Requested-With': 'XMLHttpRequest'}
78+
});
6679
6780
htmx
6881
====
@@ -74,3 +87,14 @@ You can use `ajax-header <https://github.com/bigskysoftware/htmx-extensions/blob
7487
<body hx-ext="ajax-header">
7588
...
7689
</body>
90+
91+
92+
Or you can set the header manually with ``hx-headers``:
93+
94+
.. code-block:: html
95+
96+
<button
97+
hx-post="/your-url"
98+
hx-headers='{"X-Requested-With": "XMLHttpRequest"}'>
99+
Send Request
100+
</button>

0 commit comments

Comments
 (0)