首页 > 解决方案 > 无法发送发布请求

问题描述

我正在尝试通过发布请求使用 axios 在 ReactJS 中发送数据。问题是当我发送数据时,它会出现在我的 url 中,例如“ http://127.0.0.1:8000/Sessions/11?courses=2018-09-30 ”,但我希望它进入我的数据库。

我的要求是这样的:

AddCourses (courses) {

axios.request({
method: 'post',
url: `http://127.0.0.1:8000/api/sessions/${session_id}/courses`,
data: courses,
headers: {'Content-Type': 'application/json' , 'Accept': 
'application/json' , 'Authorization': `Bearer 
${authentication.getToken()}`}

 }).catch(err => console.log(err))
}

onSubmit (event) {
let session_id = this.props.match.params.id

const newCourses = {
 date: this.refs.date.value,
}
this.AddCourses(newCourses)
event.preventDefault(
this.props.history.push(`/Courses`),
)
}

我的道具在我的路由器中是这样定义的:

   <Route path="/Sessions" exact component={Sessions}/>
   <Route path="/Sessions/:id" exact render={props => 
   <GeekSessions{...props} /> }/>

在我的组件中,我有另一个发布请求,当我测试它时,我有错误'Uncaught TypeError:无法读取未定义的属性'props'

我无法弄清楚这两个问题。如果您有任何解决方案,这将非常酷。感谢你们。

标签: reactjsreact-routeraxios

解决方案


推荐阅读