Giorgi Tsertsvadze
Giorgi Tsertsvadze

Reputation: 433

Can't sent json data to server with POST request

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 completely 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).

Upvotes: 2

Views: 4665

Answers (3)

Martin Zeitler
Martin Zeitler

Reputation: 76799

set 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');

Upvotes: 4

Ahmed Numaan
Ahmed Numaan

Reputation: 1062

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"}

Upvotes: 1

JonHayman
JonHayman

Reputation: 186

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.

Upvotes: 1

Related Questions