Vuejs帖子没有找到NodeJs Express服务器/动作

时间:2017-02-09 13:01:01

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

我正在尝试使用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没有wordk。我尝试了多个端口和文件夹,但无法弄清楚错误。

1 个答案:

答案 0 :(得分:0)

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

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

在我的情况下,我使用相同的端口用于API和前端,我不得不切换端口并允许CORS