3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】axiosでリクエストしたとき、400(Bad Request)が発生した際の対処法【axios】

Posted at

症状

Reactでaxiosを使用して、RailsAPIにアクセスしようとしたときに、400(Bad Request)が発生してしまいました。 railsAPI側のログをみると、アクセスしたいactionまで届いていそうではありますが、上手く動作していないようです。
エラー
PUT http://localhost:3000/hoges/undefined 400 (Bad Request)

axiosのコードでアクセスしていた箇所はこんな感じです。

accessPutjsx
export const url= (id) => `http://localhost:3000/hoges/${id}`;

export const accessPut() {
    hoge.id = 1
    axios.put(url(hogee.id))
}

解決策

今回はurlに設定するパラメータがミスっていたため、undefindedをそのまま送っていたため、エラーが発生していました パラメータをちゃんとurl側に送って、それを正しいurlでアクセスしたら、エラーが解消しました
accessPut.jsx
export const url= (id) => `http://localhost:3000/hoges/${id}`;

export const accessPut() {
    hoge.id = 1
    axios.put(url(hoge.id))
}
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?