i'm trying to send list of data using form_data
in react-native , but it is posting first data only , how to achieve that in react native ,
My Data ,
var data = ["person_1","person_2","person_3"];
My Code ,
export function updateUsers(data,token) {
const fd = new FormData();
fd.append('first_name', data); // here i need to update all the data
return dispatch => {
axios({
method : 'patch',
url : SERVICE.url+"user/"+un+"/",
headers: {
'Authorization' : "Token "+token,
},
data : fd
})
.then(response => {
console.log("updateUser done . . .");
var data = response.data;
dispatch({
type: "UPDATE_USER",
data,
});
}).catch(error => {
console.log("got error in UPDATE_USER ", error);
});
}
}
i'm trying to send list of data using form_data
in react-native , but it is posting first data only , how to achieve that in react native ,
My Data ,
var data = ["person_1","person_2","person_3"];
My Code ,
export function updateUsers(data,token) {
const fd = new FormData();
fd.append('first_name', data); // here i need to update all the data
return dispatch => {
axios({
method : 'patch',
url : SERVICE.url+"user/"+un+"/",
headers: {
'Authorization' : "Token "+token,
},
data : fd
})
.then(response => {
console.log("updateUser done . . .");
var data = response.data;
dispatch({
type: "UPDATE_USER",
data,
});
}).catch(error => {
console.log("got error in UPDATE_USER ", error);
});
}
}
but for now , only one data is updated at a time , so how can i achieve that .help me from this .
Share Improve this question edited Oct 23, 2017 at 14:27 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Oct 16, 2017 at 20:01 Beckham_VinothBeckham_Vinoth 7213 gold badges14 silver badges40 bronze badges 8- You have data as an array, so do you want for each item in array send a request? – Nurlan Mirzayev Commented Oct 16, 2017 at 20:24
- Yah , absolutely bro !! – Beckham_Vinoth Commented Oct 16, 2017 at 20:40
- i mean , single post request with array of data . – Beckham_Vinoth Commented Oct 16, 2017 at 20:40
- Did you try convert object to string? Connection between backend and frontend should be on string. Try data: JSON.stringify(fd) – Nurlan Mirzayev Commented Oct 16, 2017 at 20:46
- Tried bro , But no luck . – Beckham_Vinoth Commented Oct 16, 2017 at 21:58
1 Answer
Reset to default 4You should loop through each of your data and append it into your formdata's first_name field.
data.forEach((item) => {
fd.append('first_name', item)
})
The remaining code will be same.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634492a4637307.html
评论列表(0条)