简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM