【问题标题】:How use id in URL axios post method react?URL axios post方法中使用id如何反应?
【发布时间】:2019-01-24 17:11:29
【问题描述】:

我试图创建一个应用程序进行管理,但在我的应用程序的循环中,我必须使用一个进程来创建会话。

在那里,我创建了一个本地化,但之后,我需要在该位置创建一个会话。

为此,我需要在发布请求 (api/localisations/{localisation_id}/sessions) 中传递本地化的 id,但我不知道该怎么做。

当我创建一个会话时,我有这个:

AddLocalisations (localisations) {
axios.request({
  method: 'post',
  url: 'http://127.0.0.1:8000/api/localisations/',
  data: localisations,

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

onSubmit (event) {
const newLocalisations = {
  name: this.refs.name.value,
  address: this.refs.address.value,
  phone: this.refs.phone.value,

}
this.AddLocalisations(newLocalisations)
event.preventDefault(
  this.props.history.push('/AddSessions'),
)
}

【问题讨论】:

    标签: reactjs post axios


    【解决方案1】:

    试试这个

     AddLocalisations = (localisation_id, localisations) => {
      let url = "http://127.0.0.1:8000/api/localisations/" +localisation_id "/sessions"; 
      let data = {localisations };
      axios.post(url, data).then(res => console.log(res)).catch(err => console.log(err))
    

    如果它不起作用,请在项目的 package.json 文件中使用 "proxy": "http://127.0.0.1:8000",。 现在你的网址是let url = "/api/localisations/" +localisation_id "/sessions";

    【讨论】:

      【解决方案2】:

      您可以通过以下方式进行操作。当您调用 AddLocalisations 时,将 localisation_id 作为参数传递给它,并在函数中将其附加到 url 变量

       AddLocalisations (localisation_id, localisations) {
            let url = `http://127.0.0.1:8000/api/localisations/${localisation_id}/sessions`; //template literals
            axios.request({
                method: 'post',
                url: url,
                data: localisations,
                }).catch(err => console.log(err))
           }
      

      【讨论】:

      • 您认为我在创建本地化时需要这样做吗?或会议?对于 loc 它是我的问题中的代码,对于会话实际上我使用它但 id 是固定的...axios.request({ method: 'post', url: 'http://127.0.0.1:8000/api/localisations/2/sessions', data: sessions, headers: {'Content-Type': 'application/json' , 'Accept': 'application/json'} }).catch(err => console.log(err)) }
      • 我的解决方案是本地化。您可以将本地化 id 传递给处理函数并将其附加到 url
      • 当我这样做时出现此错误:请求 URL:127.0.0.1:8000/api/localisations/[object%20Object] 请求方法:POST 状态代码:405 方法不允许远程地址:127.0.0.1:8000 推荐人策略:no-referrer-when -降级
      • 您必须将本地化 id 传递给 this.AddLocalisations(localisation_id, newLocalisations) in onSubmit
      • 我按照你的指示(对不起,我是初学者),但现在我有这个错误......:请求 URL:127.0.0.1:8000/… 请求方法:GET 状态代码:404 未找到远程地址:127.0 .0.1:8000 推荐人政策:降级时无推荐人
      猜你喜欢
      • 2019-01-04
      • 1970-01-01
      • 2021-12-25
      • 2020-12-24
      • 2018-10-05
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多