【问题标题】:How to decode JSON stringify from React in a different php file如何在不同的 php 文件中从 React 解码 JSON stringify
【发布时间】:2026-01-07 02:50:02
【问题描述】:

在 React 中,我使用了 redux 并传递如下数据:

const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(user)
};
return fetch('http://localhost:8080/react-crud/api/signup.php', requestOptions).then(handleResponse);
}

function handleResponse(response) {
  return response.text().then(text => {
    const data = text && JSON.parse(text);
    if (!response.ok) {
      if (response.status === 401) {
        // auto logout if 401 response returned from api
      //  logout();
       //alert("done");
       console.log("done");
      }
      const error = (data && data.message) || response.statusText;
      return Promise.reject(error);
    }

    return data;
  });
}

在 PHP 中我调用下面的代码:

header("Content-Type: application/json; charset=UTF-8");

 $tempData = html_entity_decode($_POST['text']);
$obj = json_decode($tempData);
 //$obj = json_decode($_POST['user'],false);
 echo($_POST["user"]);
 print_r($obj);

但是没有任何东西打印它的显示错误“意外的令牌

谁能帮我解决这个问题。

【问题讨论】:

  • 我认为你应该在 PHP 端编码而不是解码
  • 我尝试了编码,但它仍然不起作用,任何想法

标签: php json reactjs react-redux


【解决方案1】:

在 PHP 解码函数中使用,如下所示。现在它工作正常:

 $formdata = json_decode(file_get_contents('php://input'), true);

$name = $formdata["name"];
$email = $formdata["email"];
$password = $formdata["password"];

【讨论】:

    最近更新 更多