我在Axios中发布包含cookie时出现网络错误

ne5o7dgx  于 2023-06-22  发布在  iOS
关注(0)|答案(2)|浏览(134)

我刚刚通过发送Cookie从localhost发出POST请求。我用Axios。405方法不允许这是我的请求源代码:

axios
  .post(
    'http://10.xxx.13.xxx/api/seller/register',
    {
      partner_id: 1,
      mothers_maiden_name: values.mothers_name,
      ktp_url: values.ktp,
      npwp_url: values.npwp,
      verification_photo_url: values.selfie,
      education: parseInt(values.last_education_degree)
    },
    {
      withCredentials: true,
      cookie:
        '_randomName_=5rimcmbr76adhajs2uTvcsG7TOniHpDbkudRd_Z6BvCGME8G5OXG3j-L8b9nMaQdH09XPw__y3noH0KYgFs'
    }
  )
  .then(function(response) {
    console.log('Success ', response)
  })
  .catch(function(error) {
    console.log('Error ', error)
  })

这是完整的错误消息
OPTIONS http://10.xxx.13.xxx/api/seller/register 405(不允许的方法)
我的代码有错误吗?希望有人能帮帮我

bxpogfeg

bxpogfeg1#

您似乎没有在header对象中设置Cookie属性。可以使用axios.post(url, data, headers)设置Cookie,如下所示:

const data = {
  partner_id: 1,
  mothers_maiden_name: values.mothers_name,
  ktp_url: values.ktp,
  npwp_url: values.npwp,
  verification_photo_url: values.selfie,
  education: parseInt(values.last_education_degree)
};

axios.post('http://10.xxx.13.xxx/api/seller/register', data, {
  headers: {
    withCredentials: true,
    Cookie: '_randomName_=5rimcmbr76adhajs2uTvcsG7TOniHpDbkudRd_Z6BvCGME8G5OXG3j-L8b9nMaQdH09XPw__y3noH0KYgFs'
  }
})
.then(function(response) {
  console.log('Success ', response)
})
.catch(function(error) {
  console.log('Error ', error)
});
8yoxcaq7

8yoxcaq72#

我不得不把我自己的像这样

const instance = axios.create({
  baseURL,
  //   withCredentials: true,
  headers: {
    withCredentials: true,
  },
});

在添加后,还得到了网络错误

相关问题