node.js - fetch跨域提交post請求,后臺node接受不到ctx.request.body?
問題描述
用fetch提交跨域的post請求,但是后臺node服務(wù)根本找不到提交的數(shù)據(jù),沒有body的屬性請求代碼如下: let url=’http://127.0.0.1:8080/manager/user/add’; fetch(url , { method: ’POST’, headers: {}, credentials: ’credentials’, cache: ’default’, body: `projectName=dddddd ` // body: insertData, }).then((response) => { if (response.ok) {return response.json(); } }).then((json) => { let userList=JSON.stringify(json); dispatch({ type:ADD_USER, userList: userList }); }).catch((error) => { console.error(error); }); } 后臺打印如下:
麻煩各位給看看!!
問題解答
回答1:header內(nèi)容要加上,以json為例子:headers: { ’Accept’: ’application/json’, ’Content-Type’: ’application/json’, }
回答2:node獲取ajax提交需要引入body-parser中間件模塊。
![css3 - [CSS] 動畫效果 3D翻轉(zhuǎn)bug](http://www.cgvv.com.cn/attached/image/news/202304/110831f073.png)