【问题标题】:Axios - How to fix - POST URL net::ERR_CONNECTION_RESETAxios - 如何修复 - POST URL net::ERR_CONNECTION_RESET
【发布时间】:2020-07-18 02:16:39
【问题描述】:

如何解决这个错误信息?

[xhr.js?b50d:178 POST http://localhost:3000/editor/add net::ERR_CONNECTION_RESET][1]

它可以工作并附加数据,但我收到此错误消息...

我的 API 如下所示:

app.js

app.post('/editor/add', function (req, res) {
let articleData; 
let textData;
let article = {
    title: req.body.title,
    content: req.body.content
}

fs.readFile(urlPath, 'utf8', (err, data) => {
    if (err) {
        console.log('readfile => ' + err);
    } else {
        articleData = JSON.parse(data);
        articleData[article.title] = article.content;
        textData = JSON.stringify(articleData, null, 2);

        fs.writeFile(urlPath, textData, 'utf8', (err) => {
            if (err) {
                console.log('write file => ' + err);
            } else {
                console.log('Finished writing');
            }
        });
    }
});

});

而我的 Axios POST 方法是这样的。

editor.vue

submitEditor: function() {
  var self = this;
  self.$axios({
      headers: {
        "Content-Type": "application/json"
      },
      method: "post",
      url: "http://localhost:3000/editor/add",
      data: {
        title: "test5",
        content: self.html
      }
    })
    .then(res => {
      console.log(res);
    })
    .catch(error => {
      if (!error.response) {
        // network error
        this.errorStatus = "Error: Network Error";
      } else {
        this.errorStatus = error.response.data.message;
      }
    });
}

我使用 Vue/cli,我将客户端代码和服务器代码分开。它们位于单独的文件夹中。我将 Vue/cli 放在我的客户端文件夹中,并将 express.js 放在我的服务器文件夹中。

提前谢谢你!

【问题讨论】:

  • 您似乎没有从您的POST 路由发送响应,这可能是问题所在。

标签: node.js express vue.js axios fs


【解决方案1】:

尝试从您的路线发送响应:

fs.writeFile(urlPath, textData, 'utf8', (err) => {
    if (err) {
        console.log('write file => ' + err);
    } else {
        console.log('Finished writing');
        res.json({ msg: 'success' }); // send the client something
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多