我正在React-native上的一个应用程序上工作,我想通过请求API将所有用户数据保存在服务器上。但是当我尝试时,我的请求有问题。
{"body": "{
\"residenceId\": \"5d88dfb55feb4c06a5cfb756\",
\"date\": \"2019-10-04T16:37:10.048Z\",
\"espaces\": [
{
\"espaceId\": \"5d88dfb55feb4c06a5cfb761\",
\"photosPath\": [
\"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-2c97392b-fd7c-4480-9d9e-d055c74cab7e.jpg\"
],
\"comment\": \"Un sol\"
},
{
\"espaceId\": \"5d88dfb55feb4c06a5cfb760\",
\"photosPath\": [
\"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-0518bb83-7d66-43f5-a0df-4e803bca50da.jpg\",
\"content://com.immodt.provider/root/storage/emulated/0/Pictures/image-d416bd03-051f-43f0-8d09-478ad616562a.jpg\"
],
\"comment\": \"Un plafond\"
}
]
}"}
我不知道为什么。
我尝试放入Stringify,尝试不使用Stringify,每当àtry我的“ espaces”数组出现stringify错误时。我认为我的数组是错误的核心。我看到了很多类似这样的bug的话题,但是没人找到我的解决方案。
我的方法使用所有数据生成我的对象(并更改体系结构)
_saveAllData() {
const token = this.props.token;
let data = this.props.picturesAndComment;
let date = new Date();
let newData = {
residenceId: this.props.residence._id,
date: date,
espaces: []
};
for (let i = 0, c = data.length; i < c; i++) {
newData.espaces[i] = {
espaceId: data[i].idSubspace,
photosPath: [],
comment: data[i].comment
};
for (let j = 0, d = data[i].pictures.length; j < d; j++) {
newData.espaces[i].photosPath[j] = data[i].pictures[j].uri;
}
}
console.log(newData);
//this.setState({ isLoading: true });
/*Promise.all([postVisit(token, newData), postPhoto(token, picturesDataForUpload)]).then((arrayOfResponses) => {
console.warn(arrayOfResponses);
this.setState({ isLoading: false });
}).catch((error) => {
console.warn('Error :', error);
this.setState({ isLoading: false });
});*/
}
}
我的发送数据的API方法
export function postVisit(token, data) {
const url = "http://51.91.11.5:8080/visites/";
const request = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token.accessToken
},
body: data
};
console.log(request);
return fetch(url, request)
.then((response) => response.json());
}
我希望普通的JSON到处都没有反斜杠...
问题已解决。关心的是我客户的API,而不是我的代码。谢谢您的回答。