您的位置:首頁技術文章
文章詳情頁

node.js - fetch跨域提交post請求,后臺node接受不到ctx.request.body?

瀏覽:87日期:2024-07-17 08:20:14

問題描述

用fetch提交跨域的post請求,但是后臺node服務根本找不到提交的數據,沒有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); }); } 后臺打印如下:

node.js - fetch跨域提交post請求,后臺node接受不到ctx.request.body?

麻煩各位給看看!!

問題解答

回答1:

header內容要加上,以json為例子:headers: { ’Accept’: ’application/json’, ’Content-Type’: ’application/json’, }

回答2:

node獲取ajax提交需要引入body-parser中間件模塊。

国产综合久久一区二区三区