1

I need to submit an ARRAY data because the backend can only recognize such data

Expected effect:

&row[weigh]=0
&row[status]=normal

code:

row:{
  weigh: 0,
  status: 'normal'
}

actual effect:

row:{
  weigh: 0,
  status: 'normal'
}

When I submit the data, the console displays JSON instead of Array, but the backend cannot get it

What I need is to be consistent with the results from the form submission below

<form  method="POST" >
  <input name="row[a]" type="text" value="">
  <input name="row[b]" type="text" value="">

4 Answers 4

1
public register(rowObject: RowObject): AxiosPromise<any> {
    return axios.post('http://localhost/api/register', rowObject);
}

This way you can pass the data in Post method.

rowObject =  {
    weigh: 0,
    status: 'normal'
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code.

let row = {
  weigh: 0,
  status: 'normal'
};

let finalArr = [];

Object.keys(row).forEach((key) => {
    finalArr.push(`row[${key}]=` + row[key]);
});
console.log(finalArr.join('&'));
// outputs: row[weigh]=0&row[status]=normal

Comments

0

Your code also should pass an array just like this.

data = [
  {weigh: 0},
  {status: 'normal'}
]

then when you send it to server for example using axios, your code should look like this

axios.post('/api endpoint', {row:data})
    .then(response => {
     // response here
});

Comments

0

ok

const formData = new FormData()
  Object.keys(this.form).forEach(e => {
  formData.append(`row[${e}]`, this.form[e])
})

1 Comment

Please add some explanation to your answer by editing it such that others can learn from it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.