我正在制作一个由单独的前端和后端组成的项目。从前端,我通过 fetch 发出一个 POST 请求,该请求应该向后端发送一个字符串“ORANGE”,然后后端应该将其记录到控制台。我无法让后端控制台记录字符串。我查看了 devtools 中的请求,字符串“ORANGE”被埋在“Request payload”下。请求本身发送正常。我如何实际访问字符串以便我可以用它做事?(例如,存储在数据库中)
//FRONTEND
const commentForm = document.getElementById("editform");
commentForm.addEventListener('submit', function(e) {
e.preventDefault();
fetch('http://localhost:3000/posts/:id', {
mode: 'cors',
method: 'post',
headers: {
"Content-type": "text/plain;charset=UTF-8"
},
body: "ORANGE"
}).then(function(response) {
if (response.ok) {
console.log("response.ok was true: "+ response)
} else {
let error = new Error(response.statusText)
error.response = response
throw error
}
})
});
//BACKEND
router.post('/posts/:id', function(req, res, next) {
console.log('What do I put here to get ORANGE logged?!')
//On the server side I tried some console.log tests.
//console.log("req is " + req); //req is [object Object]
//console.log("type of req is " + typeof req); //type of req is object
//console.log(JSON.parse(req)); //SyntaxError: unexpected token o in JSON at position 1
res.send('whatever. I want ORANGE.')
}
红颜莎娜
摇曳的蔷薇
互换的青春
相关分类