【问题标题】:Vuejs post does not find NodeJs Express server/actionVuejs 帖子未找到 NodeJs Express 服务器/操作
【发布时间】:2017-02-09 13:01:01
【问题描述】:

我正在尝试使用 vue.js 将数据发送到我的 node.js 服务器,但浏览器控制台一直显示 404:POST http://127.0.0.1:63342/myaction 404 (Not Found)

vue.js:

this.$http.post('http://127.0.0.1:63342/myaction', this.formData).then(response => {
    console.log(response.body);
}

node.js:

var express = require('express');
var bodyParser = require('body-parser');
var exp = express();

exp.use(bodyParser.urlencoded({extended: true}));

exp.post('/myaction', function (req, res) {
    res.send('saved: "' + req.body.name + '".');    
});

exp.listen(63342, function () {
    console.log('Server running at', this.address());
});

当我启动我的服务器时,它说它正在{ address: '::', family: 'IPv6', port: 63342 } 运行

POST 在没有 vue.js 的情况下工作,只需提交 HTML form,但现在 AJAX 不支持。我尝试了多个端口和文件夹,但无法找出错误。

【问题讨论】:

    标签: javascript ajax node.js post vue.js


    【解决方案1】:

    我终于明白了。您必须手动向侦听器添加地址:

    app.listen(63342, '127.0.0.1', function () {}

    在我的情况下,我为 API 和前端使用相同的端口,我必须切换端口并允许 CORS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2023-03-19
      • 2015-02-13
      相关资源
      最近更新 更多