我正在使用访存方法来发送和api调用,但是请求参数采用由String插入的JSON格式。这是我的代码,请纠正我做错的地方
export function RestApi(data) {
let BaseUrl = "http://localhost:8000/api/login";
return new Promise((resolve, reject) => {
fetch(BaseUrl, {
method: "POST",
body: JSON.stringify(data)
})
.then(response => response.json())
.then(responseJson => {
resolve(responseJson);
})
.catch(error => {
reject(error);
});
});
}
this.state = {
username: "",
password: ""
};
RestApi(this.state).then(result => {});
答案 0 :(得分:0)
您的问题有点不清楚,如果您想以纯文本形式发送,则可以按如下所示设置标头content-type。
fetch(BaseUrl, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "text/plain"
}
});
编辑
实际上,发送JSON字符串应始终使用application/json
fetch(BaseUrl, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
});