I am trying to send json data to server (using fetch API and PHP as a server side language). My server side code is pretty simple:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
print_r($_POST);
?>
Now when I send the request simply with "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
like this:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: "a=b"
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
Everything works fine and the output is:
Array
(
[a] => b
)
Now when I want to send the same thing but with JSON like this:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: JSON.stringify({"a":"b"})
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
I get the weird output of the whole array as a key:
Array
(
[{"a":"b"}] =>
)
Now when I change the content type to: "application/json"
in fetch call, the output is pletely lost and I get the empty array:
Array
(
)
Can you tell me what the reason is? and how to achieve the desired result. (sending whole data with JSON).
I am trying to send json data to server (using fetch API and PHP as a server side language). My server side code is pretty simple:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
print_r($_POST);
?>
Now when I send the request simply with "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
like this:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: "a=b"
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
Everything works fine and the output is:
Array
(
[a] => b
)
Now when I want to send the same thing but with JSON like this:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: JSON.stringify({"a":"b"})
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
I get the weird output of the whole array as a key:
Array
(
[{"a":"b"}] =>
)
Now when I change the content type to: "application/json"
in fetch call, the output is pletely lost and I get the empty array:
Array
(
)
Can you tell me what the reason is? and how to achieve the desired result. (sending whole data with JSON).
Share Improve this question asked Aug 5, 2018 at 14:51 Giorgi TsertsvadzeGiorgi Tsertsvadze 4431 gold badge9 silver badges23 bronze badges 4- possible duplicate from - take a look https://stackoverflow./questions/29775797/fetch-post-json-data – netzding Commented Aug 5, 2018 at 14:56
- @PhilS Not, just tried that and it still gives the empty output. – Giorgi Tsertsvadze Commented Aug 5, 2018 at 15:11
-
did you use
FormData
? – Sakezzz Commented Aug 5, 2018 at 15:21 - @Sakezzz yes, and that worked, but I still want to know what is the problem here. – Giorgi Tsertsvadze Commented Aug 5, 2018 at 15:23
3 Answers
Reset to default 4set the content-type to application/json
:
fetch('http://localhost:80/test.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({a: 'b'})
});
and the server-side should be able to decode (there is no $_POST
without a form's post-fields):
$array = json_decode(file_get_contents('php://input'));
echo '<pre>'.print_r($array, true).'</pre>';
the test.php
should jut send a content type header, along with the JSON:
header('Content-type: application/json; charset=utf-8');
Your JSON should be like:
JSON.stringify({a: 'Text Value', b: 1})
Then in your PHP:
print_r(json_decode($_POST));
PHP doesn't understand JSON until you decode it.
You code below:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: JSON.stringify({"a":"b"})
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
Should be written as:
fetch("http://localhost:80/test.php", {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: {"a":"b"}
})
.then(function(response){
return response.text();
})
.then(function(text){
console.log(text);
})
Note:
body: JSON.stringify({"a":"b"})
has been changed to body: {"a":"b"}