【问题标题】:axios Post request returns null responseaxios Post 请求返回空响应
【发布时间】:2019-09-04 01:12:33
【问题描述】:

我遇到的问题是我发出的 axios POST 请求收到 NULL 响应。如何更正此错误?

我尝试将 multipart/form-data 更改为 x-www-form-urlencoded 和 multipart/x-www-form-urlencoded

async submitUser() {
  this.memberObj = {
    id: this.memberObjLocal.id,
    name: this.memberObjLocal.name,
    password: this.memberObjLocal.password,
    email: this.memberObjLocal.email,
    password: this.memberObjLocal.password,
    gender: this.memberObjLocal.gender,
    experience: this.memberObjLocal.experience,
    birth: this.memberObjLocal.birth,
    phoneNum: this.memberObjLocal.phoneNum,
    address: this.address,
    introduce: this.introduce,
    pic: this.pic
  };
  const config = {
    headers: { "Content-Type": "multipart/form-data" }
  };
  var formData = new FormData();
  for (let data in this.memberObj) {
    console.log(data); 
    formData.append(data, this.memberObj[data]);
    console.log(this.memberObj[data]); 
  }
  for (var key of formData.entries()) {
    console.log(key[0] + ", " + key[1]); 
  }
  try {
    let response = await this.$axios.$post("/users/", formData, config);
    this.$router.push("/");
    console.log(response) // null
  } catch (e) {
    console.log(e.response);
  }
}

【问题讨论】:

  • 我已经确认了 formData 的值。
  • 你检查过payload是否包含数据吗?
  • 您是否收到错误或响应的正文为空?如果是后者,那么您的问题可能出在后端(或后端/前端不兼容) - 这个 axios POST 命中了什么?您在服务器日志中看到了什么?

标签: django vue.js nuxt.js


【解决方案1】:

请尝试这样使用

import axios from "axios";

//... your codes here

    axios({
      url: "/users/",
      method: "post",
      data: formData,
      headers: config.headers
    })
    .then(response => {
      this.$router.push("/");
      console.log(response);
    })
    .catch(error => console.error(error));

//... and other codes here

你可以看到more examples

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 2020-12-18
    • 2020-04-28
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多