$_REQUEST gets parameters but not $_POST when using fetch api
I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:
fetch(url, {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));
});
Where the query string of the url has the form: action=login&tokend_id=1234
And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.
Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.
I checked whether $_POST is empty with this statement:
if ( isset( $_POST ) && 0 === count($_POST) )
Thanks
php ajax post fetch
add a comment |
I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:
fetch(url, {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));
});
Where the query string of the url has the form: action=login&tokend_id=1234
And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.
Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.
I checked whether $_POST is empty with this statement:
if ( isset( $_POST ) && 0 === count($_POST) )
Thanks
php ajax post fetch
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43
add a comment |
I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:
fetch(url, {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));
});
Where the query string of the url has the form: action=login&tokend_id=1234
And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.
Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.
I checked whether $_POST is empty with this statement:
if ( isset( $_POST ) && 0 === count($_POST) )
Thanks
php ajax post fetch
I am using fetch to send a POST request to the backend 'ajax-admin.php' which then redirects to a php function I have in functions.php.
I decided to replace xmlHttpRequests with 'fetch' api.
I do a post request with fetch like this:
fetch(url, {
method: 'POST',
credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.json())
.then(data => resolve(data)) // we call resolve with the data
.catch(err => reject(err));
});
Where the query string of the url has the form: action=login&tokend_id=1234
And on the server side I am using $_POST to retrieve the value of the parameters 'action' and 'token_id'.
However what I see is that $_POST is empty while $_REQUEST does have the paremeters. So if I do $_POST['token_id'] I get an error because $_POST is empty. But if I do $_REQUEST['token_id'] , I get the value of that parameter.
Any idea why this is happening?
Why would the parameters be in $_REQUEST but not $_POST.
I checked whether $_POST is empty with this statement:
if ( isset( $_POST ) && 0 === count($_POST) )
Thanks
php ajax post fetch
php ajax post fetch
asked Nov 22 '18 at 2:39
malenamalena
345219
345219
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43
add a comment |
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43
add a comment |
1 Answer
1
active
oldest
votes
Where the query string of the url...
What you're actually doing here is sending a POST
request to a URL with a query string. The query string is parsed and placed into $_REQUEST
and $_GET
regardless of the actual HTTP method used. However, $_POST
can only be populated with an actual POST
request with a request body.
In your fetch()
call, specify body: formData
. formData
can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.
– Brad
Nov 23 '18 at 5:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423125%2frequest-gets-parameters-but-not-post-when-using-fetch-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Where the query string of the url...
What you're actually doing here is sending a POST
request to a URL with a query string. The query string is parsed and placed into $_REQUEST
and $_GET
regardless of the actual HTTP method used. However, $_POST
can only be populated with an actual POST
request with a request body.
In your fetch()
call, specify body: formData
. formData
can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.
– Brad
Nov 23 '18 at 5:21
add a comment |
Where the query string of the url...
What you're actually doing here is sending a POST
request to a URL with a query string. The query string is parsed and placed into $_REQUEST
and $_GET
regardless of the actual HTTP method used. However, $_POST
can only be populated with an actual POST
request with a request body.
In your fetch()
call, specify body: formData
. formData
can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.
– Brad
Nov 23 '18 at 5:21
add a comment |
Where the query string of the url...
What you're actually doing here is sending a POST
request to a URL with a query string. The query string is parsed and placed into $_REQUEST
and $_GET
regardless of the actual HTTP method used. However, $_POST
can only be populated with an actual POST
request with a request body.
In your fetch()
call, specify body: formData
. formData
can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body
Where the query string of the url...
What you're actually doing here is sending a POST
request to a URL with a query string. The query string is parsed and placed into $_REQUEST
and $_GET
regardless of the actual HTTP method used. However, $_POST
can only be populated with an actual POST
request with a request body.
In your fetch()
call, specify body: formData
. formData
can be represented in this querystring format, or in other ways. See also: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Body
answered Nov 22 '18 at 3:21
BradBrad
115k27232393
115k27232393
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.
– Brad
Nov 23 '18 at 5:21
add a comment |
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.
– Brad
Nov 23 '18 at 5:21
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
So any idea then. why when I use xmlHttpRequest POST request , passing the parameters again as part of the query string part of the URL , I actually can retrieve the values with $_POST. This is what is confusing. I would expect fetch and xmlHttpRequest to behave the same way on a POST request but they don't.
– malena
Nov 23 '18 at 4:33
@malena If PHP and your webserver are configured correctly,
$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.– Brad
Nov 23 '18 at 5:21
@malena If PHP and your webserver are configured correctly,
$_POST
will never be populated from the querystring itself. It will only be populated from a parsed POST request body.– Brad
Nov 23 '18 at 5:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423125%2frequest-gets-parameters-but-not-post-when-using-fetch-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
REQUEST has both post and get, as you see the values in the url, GET is being used not POST
– user10226920
Nov 22 '18 at 2:42
Because, you're doing a GET request and that won't interact with a POST method (and vice-versa).
– Funk Forty Niner
Nov 22 '18 at 2:43