Axios: لماذا يتعين علينا استخدام وحدة سلسلة الاستعلام؟

تم إنشاؤها على ٤ مارس ٢٠١٧  ·  3تعليقات  ·  مصدر: axios/axios

قضيت يومًا كاملاً تقريبًا في محاولة لمعرفة سبب إرسال Axios لبياناتي بتنسيق خاطئ حتى وجدت هذا http://stackoverflow.com/questions/31756756/axios-http-client-how-to-construct-http-post- عنوان url مع شكل المعلمات. الإصلاح هو استخدام وحدة سلسلة الاستعلام.

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);
});

ومع ذلك ، أشعر أن أكسيوس يجب أن تهتم بهذا الأمر.

التعليق الأكثر فائدة

يمكنك استخدام 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 كومينتر

توجد بالفعل تعليمات في الملف التمهيدي ، لذلك كان من السهل جدًا العثور على الإجابة:

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 التقييمات