【发布时间】:2020-12-13 21:18:01
【问题描述】:
我认为请求没有问题,因为我使用了相同的请求发布新角色并且它成功了。 我认为问题在于响应。
使用 Express 和 Node 的后端
我有一个 API 路由,它更新一个字符。
router.put('/:id', (req, res, next) => {
const { id } = req.params;
Promise.all([
updateAbilities(id, req.body.abilities, next),
updateCharacter(id, req.body.name, req.body.img, next),
updateShows(id, req.body.shows, next)
])
.then(values => console.log(values, 'are the values received at put request'))
.then(() => res.redirect('/' + id))
.catch(err => next(err));
})
当我使用 Postman 发送请求时,我得到了完美的响应。
使用 Axios 的 React App 前端
我正在使用 Axios 在 React 应用上执行相同的请求。
export async function updateOne(inputs, cid) {
inputs = {
name: "green latern 2",
abilities: "ring, superpower",
shows: "justice league",
img: "an image",
};
cid = 11;
let err = null
const response = await axios({
method: 'PUT',
url: BASE_URL + cid,
data: inputs
}).catch(e => err = e);
console.log(response, 'was the response');
return;
}
但是这样做时,我收到了这个错误:
Error: Network Error
createError createError.js:16
handleError xhr.js:83
dispatchXhrRequest xhr.js:80
xhrAdapter xhr.js:12
dispatchRequest dispatchRequest.js:52
promise callback*request Axios.js:61
wrap bind.js:9
updateOne apiCalls.js:36
js apiCalls.js:66
Webpack 22
was the response apiCalls.js:42
代码可以在 GitHub 上找到:https://github.com/uinstinct/card_builder
如果您需要更多信息,我会提供您所需要的。请对相同的评论发表评论
【问题讨论】:
-
你能在网络标签中查看吗? api是否命中?
-
BASE_URL的值是多少,也在邮递员中发送 x-www-form-urlencoded 但在您的代码中它是一个普通的 js 对象。 -
@Reyno
BASE_URL的值是http://localhost:9000/并且工作正常
标签: javascript node.js reactjs express axios