通过React JS/Axios发送的值在PHP后端接收时被删除

csbfibhn  于 2023-04-30  发布在  iOS
关注(0)|答案(1)|浏览(117)

React JS中的JSON值叫做formdata

{
    account_id: account_id? account_id : "null",
    name: "",
    email: "",
    password: "",
    is_active: true,
    filters:{
      minBlockRate: 20,
      minRatePerHour: 10,
      hours: {
        startTime: "00:00",
        endTime: "23:55",
      },
      weekdays: {
        Monday: true,
        Tuesday: true,
        Wednesday: true,
        Thursday: true,
        Friday: true,
        Saturday: true,
        Sunday: true
      },
      warehouses: [],
    },
    timezone: "America/New_York",
  }

这是通过Axios发送的

axios.post(ProjectConfig.api_url, null,
      {
        headers:{
          'LTKN': cookies.get('ltkn'),
        },
        params: {
          request: 'login',
          formdata: formValues
        }
      })
        .then((response) => {
.... REST OF CODE

当PHP API接收到这个消息时,键/值仓库被删除。
这是如何接收值的方法。

case 'login':
$formdata = $_REQUEST['formdata'];
$manual_token = $_REQUEST['manual_token'];

如果我对$formdata执行json_encode,结果是
{ "account_id":"null", "name":"Juan", "email":"ambarmalave@yahoo.com", "password":"123", "is_active":"true", "filters":{ "minBlockRate":"20", "minRatePerHour":"10", "hours":{ "startTime":"00:00", "endTime":"23:55" }, "weekdays":{ "Monday":"true", "Tuesday":"true", "Wednesday":"true", "Thursday":"true", "Friday":"true", "Saturday":"true", "Sunday":"true" } }, "timezone":"America\/New_York" }
我不知道这里的问题在哪里。React或PHP
附加信息。

  • React js / axios,像JSON一样发送JSON对象,所以,我不需要使用json_decode来格式化它。
kqqjbcuj

kqqjbcuj1#

当然,json对象应该被POST并作为主体接收,而不是作为参数。

axios.post(ProjectConfig.api_url, formValues,
      {
        headers:{
          'LTKN': cookies.get('ltkn'),
        },
        params: {
          request: 'login'
        }
      })

使用PHP接收如下:

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

相关问题