Axios: なぜクエリ文字列モジュールを使用する必要があるのですか?

作成日 2017年03月04日  ·  3コメント  ·  ソース: axios/axios

このhttp://stackoverflow.com/questions/31756756/axios-http-client-how-to-construct-http-post-を見つけるまで、Axiosが間違った形式でデータを送信していた理由を理解するためにほぼ一日を費やしました

axios.post(
  '/api/v1/auth/login',
  querystring.stringify({ // <-- this is what fixed it. JSON.stringify didn't help here
    user_id: this.state.user_id,
    password: this.state.password,
    _csrf: result.data.csrfToken
  }),
  {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
.then((res) => {
  if (res.data && res.data) {
    console.log('here')
  }
})
.catch((err) => {
  console.log(err);
});

ただし、Axiosがこれを処理する必要があると思います。

最も参考になるコメント

paramsを使用してクエリ文字列を指定できます。

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },

全てのコメント3件

readmeにはすでに説明が含まれているので、答えを見つけるのは非常に簡単だったはずです。

https://github.com/mzabriskie/axios#using -applicationx-www-form-urlencoded-format

Axiosがこれを自動的に処理するかどうかは私が答える質問ではありませんが、ライブラリの主な焦点はJSONの送信であるため、そのユースケースに最適化されていると思います。

ああ、そのリンクをありがとう。 私は前にそれを見ませんでした。

paramsを使用してクエリ文字列を指定できます。

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },
このページは役に立ちましたか?
0 / 5 - 0 評価