[英]Fetch http POST request sending EMPTY object to Node server
我正在嘗試從 html 表單向我的節點服務器發出 HTTP 發布請求。 我使用 Body Parser 和所有東西,但是當我嘗試獲取請求並將 req.body 記錄在我的服務器中時,它是一個 EMPTY object。 你知道我該如何解決這個問題嗎?
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const form = document.querySelector('form');
form.addEventListener('submit', e => {
e.preventDefault();
const email = document.querySelector('#email').value;
const password = document.querySelector('#password').value;
const formData = { email, password };
const options = {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(formData),
mode: 'no-cors'
};
fetch('http://localhost:3000/users/login', options)
.then(res => {
console.log(res);
res.json();
})
.then(data => {
console.log(data);
});
你說mode: 'no-cors'
所以所有需要 CORS 權限的東西都會被忽略(包括將Content-Type
header 設置為application/json
)。
刪除mode: 'no-cors'
(並使用服務器上的cors 中間件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.