axios发布请求返回空响应

时间:2019-09-04 01:12:33

标签: django vue.js nuxt

我面临一个针对我发出的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);
  }
}

1 个答案:

答案 0 :(得分:0)

请尝试这样使用

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