首页 > 解决方案 > axios Post 请求返回空响应

问题描述

我面临的问题是我发出的 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);
  }
}

标签: djangovue.jsnuxt.js

解决方案


请尝试像这样使用

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

你可以看到更多的例子


推荐阅读