【问题标题】:I get Network Error When Post include cookie in Axios在 Axios 中发布包含 cookie 时出现网络错误
【发布时间】:2019-02-14 14:36:08
【问题描述】:

我刚刚通过发送 Cookie 从本地主机发出 POST 请求。我使用 Axios。我得到了 405 Method Not Allowed。这是我的请求源代码:

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

这是完整的错误信息

选项http://10.xxx.13.xxx/api/seller/register 405(方法不允许)

我的代码有什么错误吗?希望有人可以帮助我

【问题讨论】:

    标签: node.js axios


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 1970-01-01
      • 2018-08-14
      • 2020-10-26
      • 2022-09-30
      • 2020-04-23
      • 2021-03-26
      • 2020-03-16
      • 2019-10-19
      相关资源
      最近更新 更多