axios 没有执行 post 请求
axios is not implementing post request
我正在尝试使用 axios(NodeJS、ReactJS)post 数据,但我最终遇到了这个错误
这是我的 posting 代码
axios({
method: 'post',
url: '/api/signup',
data:
{
username: this.state.username,
name: this.state.name,
surname: this.state.surname,
email: this.state.email,
password: this.state.password,
confirm_password: this.state.confirm_password,
}
})
.then(res => res.data)
.then(url => window.location.href = url.location)
.catch(error => this.setState({errorBol: true, errorMessage: error}))
和我的 nodeJS 代码
router.post('/', async (req,res)=>{
const username = req.body.username;
const name = req.body.name;
const surname = req.body.surname;
const email = req.body.email;
const password = req.body.password;
const confirm_password = req.body.confirm_password;
console.log(username)
console.log(name)
console.log(surname)
console.log(email)
console.log(password)
console.log(confirm_password)
res.status(200).send({location: '/'})
})
我有这样的 /api/signup 配置
router.use('/api/main', require('./sinupfilename'))
所以问题不在 router.post('/')
关于问题:
我正在实施 post 请求表单提交并对表单进行验证,它工作得很好,但是当我点击提交按钮时它给我一个错误,所以如果有人知道线索,我会很高兴听到它
看起来像 issue is not with axios but with your render function
。当您尝试渲染任何对象而不是有效的反应元素时,就会出现上述问题。
问题可能出在 errorMessage 变量的 setState 处。尝试打印 errorMessage 或 typeof errorMessage 以获取更多信息。它不应该是一个对象。
如果您阅读了 axios 的 official documentation,则错误是 javascript object
。
您需要提取错误消息并将其设置在您的 errorMessage 变量中。它应该工作正常。根据文档,可以通过类似的方式完成相同的操作:
const err = ""
if (error.response) {
err = error.response.data
} else if (error.request) {
err = error.request.response
} else {
err = error.message
}
this.setState({errorBol: true, errorMessage: err})
基本上,任何需要渲染的东西都必须是有效的反应元素,如字符串、html 标签、数字但不是对象。因此,您需要确保呈现的任何内容都必须是有效的 React 元素。您可以阅读更多相关信息 here
希望对您有所帮助,如有疑问请回复。
我正在尝试使用 axios(NodeJS、ReactJS)post 数据,但我最终遇到了这个错误
这是我的 posting 代码
axios({
method: 'post',
url: '/api/signup',
data:
{
username: this.state.username,
name: this.state.name,
surname: this.state.surname,
email: this.state.email,
password: this.state.password,
confirm_password: this.state.confirm_password,
}
})
.then(res => res.data)
.then(url => window.location.href = url.location)
.catch(error => this.setState({errorBol: true, errorMessage: error}))
和我的 nodeJS 代码
router.post('/', async (req,res)=>{
const username = req.body.username;
const name = req.body.name;
const surname = req.body.surname;
const email = req.body.email;
const password = req.body.password;
const confirm_password = req.body.confirm_password;
console.log(username)
console.log(name)
console.log(surname)
console.log(email)
console.log(password)
console.log(confirm_password)
res.status(200).send({location: '/'})
})
我有这样的 /api/signup 配置
router.use('/api/main', require('./sinupfilename'))
所以问题不在 router.post('/')
关于问题:
我正在实施 post 请求表单提交并对表单进行验证,它工作得很好,但是当我点击提交按钮时它给我一个错误,所以如果有人知道线索,我会很高兴听到它
看起来像 issue is not with axios but with your render function
。当您尝试渲染任何对象而不是有效的反应元素时,就会出现上述问题。
问题可能出在 errorMessage 变量的 setState 处。尝试打印 errorMessage 或 typeof errorMessage 以获取更多信息。它不应该是一个对象。
如果您阅读了 axios 的 official documentation,则错误是 javascript object
。
您需要提取错误消息并将其设置在您的 errorMessage 变量中。它应该工作正常。根据文档,可以通过类似的方式完成相同的操作:
const err = ""
if (error.response) {
err = error.response.data
} else if (error.request) {
err = error.request.response
} else {
err = error.message
}
this.setState({errorBol: true, errorMessage: err})
基本上,任何需要渲染的东西都必须是有效的反应元素,如字符串、html 标签、数字但不是对象。因此,您需要确保呈现的任何内容都必须是有效的 React 元素。您可以阅读更多相关信息 here
希望对您有所帮助,如有疑问请回复。