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: user_guide_src/source/general/ajax.rst
+42-18Lines changed: 42 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
AJAX Requests
3
3
##############
4
4
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 reliablewithout additional configuration.
6
6
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.
8
8
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.
10
10
11
11
.. contents::
12
12
:local:
@@ -28,41 +28,54 @@ Fetch API
28
28
Axios
29
29
=====
30
30
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.
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:
43
40
44
41
.. code-block:: javascript
45
42
46
-
$.ajax({
47
-
url:"your url",
48
-
headers: {'X-Requested-With':'XMLHttpRequest'}
43
+
constapi=axios.create({
44
+
headers: {
45
+
'X-Requested-With':'XMLHttpRequest'
46
+
}
49
47
});
50
48
51
-
VueJS
52
-
=====
53
49
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:
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:
0 commit comments