Skip to content

Commit 5ca0d22

Browse files
authored
Merge pull request #2526 from jlamim/user_guide
Page in the official documentation on ajax requests with iSAJAX() fixes #2454
2 parents 6d4e847 + 43b7c9a commit 5ca0d22

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

user_guide_src/source/concepts/http.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ is an object-oriented representation of the HTTP request. It provides everything
9797
The request class does a lot of work in the background for you, that you never need to worry about.
9898
The ``isAJAX()`` and ``isSecure()`` methods check several different methods to determine the correct answer.
9999

100+
.. note:: The ``isAJAX()`` method depends on the ``X-Requested-With`` header, which in some cases is not sent by default in XHR requests via JavaScript (i.e. fetch). See the AJAX Requests </general/ajax> section on how to avoid this problem.
101+
102+
::
103+
100104
CodeIgniter also provides a :doc:`Response class </outgoing/response>` that is an object-oriented representation
101105
of the HTTP response. This gives you an easy and powerful way to construct your response to the client::
102106

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
##############
2+
AJAX Requests
3+
##############
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.
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.
8+
9+
Here's how to force the ``X-Requested-With`` header to be sent in the Fetch API and other JavaScript libraries.
10+
11+
Fetch API
12+
=========
13+
14+
fetch(url, {
15+
method: "get",
16+
headers: {
17+
"Content-Type": "application/json",
18+
"X-Requested-With": "XMLHttpRequest"
19+
}
20+
21+
22+
jQuery
23+
======
24+
25+
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:
26+
27+
$.ajax({
28+
url: "your url",
29+
headers: {'X-Requested-With': 'XMLHttpRequest'}
30+
});
31+
32+
33+
VueJS
34+
=====
35+
36+
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.
37+
38+
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
39+
40+
41+
React
42+
=====
43+
44+
axios.get("your url", {headers: {'Content-Type': 'application/json'}})

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ be checked with the ``isAJAX()`` and ``isCLI()`` methods::
7171
. . .
7272
}
7373

74+
.. note:: The ``isAJAX()`` method depends on the ``X-Requested-With`` header, which in some cases is not sent by default in XHR requests via JavaScript (i.e. fetch). See the AJAX Requests </general/ajax> section on how to avoid this problem.
75+
76+
::
77+
7478
You can check the HTTP method that this request represents with the ``method()`` method::
7579

7680
// Returns 'post'

0 commit comments

Comments
 (0)