我这样调用网络服务:
fetch('url', {
method: 'POST',
headers: new Headers({
Accept: 'application/json',
'Content-Type': 'application/json', // <-- Specifying the Content-Type
}),
})
.then((response) => response.text())
.then(leaders => {
console.log("leader = ", leaders);
}
但是这给了我这样的错误:
Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.
答案 0 :(得分:0)
尝试以下代码:
fetch('url', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(collection) // <-- Post parameters
})
.then((response) => response.text())
.then(leaders => {
console.log("leader = ", leaders);
}
或者您可以直接致电:
fetch(url)
.then(response => response.json())
.then(data => this.setState({ data }));