【问题标题】:Can't sent json data to server with POST request无法使用 POST 请求将 json 数据发送到服务器
【发布时间】:2018-08-05 14:51:56
【问题描述】:

我正在尝试将 json 数据发送到服务器(使用 fetch API 和 PHP 作为服务器端语言)。我的服务器端代码非常简单:

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

现在当我像这样使用"Content-type": "application/x-www-form-urlencoded; charset=UTF-8" 发送请求时:

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

一切正常,输出为:

Array
(
    [a] => b
)

现在,当我想发送相同的东西但使用 JSON 时:

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

我得到了整个数组的奇怪输出作为键:

Array
(
    [{"a":"b"}] => 
)

现在,当我在 fetch 调用中将内容类型更改为:"application/json" 时,输出完全丢失,我得到空数组:

Array
(
)

你能告诉我是什么原因吗?以及如何达到预期的效果。 (使用 JSON 发送整个数据)。

【问题讨论】:

标签: javascript php json ajax fetch


【解决方案1】:

将内容类型设置为application/json:

fetch('http://localhost:80/test.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({a: 'b'})
});

并且服务器端应该能够解码(没有表单的后字段就没有$_POST):

$array = json_decode(file_get_contents('php://input'));
echo '<pre>'.print_r($array, true).'</pre>';

test.php 应该与 JSON 一起发送内容类型标头:

header('Content-type: application/json; charset=utf-8');

【讨论】:

    【解决方案2】:

    您的 JSON 应该是这样的:
    JSON.stringify({a: 'Text Value', b: 1})

    然后在你的 PHP 中:
    print_r(json_decode($_POST));

    在您解码 JSON 之前,PHP 无法理解它。

    【讨论】:

      【解决方案3】:

      你的代码如下:

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

      应该写成:

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

      注意: body: JSON.stringify({"a":"b"}) 已更改为 body: {"a":"b"}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-31
        • 1970-01-01
        • 2014-03-22
        • 2023-03-15
        • 2016-07-03
        • 1970-01-01
        • 2015-06-11
        • 2013-07-29
        相关资源
        最近更新 更多