JS:具有大型嵌套对象和表单数据的axios POST

问题描述 投票:0回答:2

我是post一个Axios请求,因为使用get会导致414错误。这是对象:

rows= {
  0 : {
    "name":"Thor",
    "status":"active",
    "email":"[email protected]",
  },
  1 : {
    "name":"Mesa",
    "status":"active",
    "email":"[email protected]",
  },
  2 : {
    "name":"Jesper",
    "status":"stdby",
    "email":"[email protected],
  },
}

这只是对象格式的示例。实际元素中有400+个元素,因此是post而不是get。我在此上正确构建表单数据时遇到麻烦。这是我所拥有的:

let data = new FormData();
Object.keys(rows).forEach(key => data.append(key, rows[key]));  //  <--- this doesn't do
data.set('target', target);  //  <---- this comes through just fine

axios({
  method: 'post',
  url: 'byGrabthorsHammer.php',
  data: data,
  headers: {'Content-Type': 'multipart/form-data'}
}).then(function(response) {
  if (response.error) {
    console.log('failed to send list to target');
    console.log(response);
  } else {
    console.log('response: ');
    console.log(response);
  }        
});

只是[Object][Object]' when i var_dump($ _ POST);`。这不是我想要的。我该如何正确地重写它,以便将数据传输到另一端(例如GET ...)。

javascript post axios form-data
2个回答
0
投票
您可以尝试对数据进行字符串化。 JSON.stringify(data)

0
投票
Yow兄弟,```POSTAre for inserting new stuff, instead of doing a post you need a patch axios.patch it is basically the same. And it won’t fix your issue. To fix the issue you need to set theContent-Type`到application / json,然后是yow
© www.soinside.com 2019 - 2024. All rights reserved.