繁体   English   中英

Vue.js帖子找不到NodeJs Express服务器/操作

[英]Vuejs post does not find NodeJs Express server/action

我正在尝试使用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不再使用。 我尝试了多个端口和文件夹,但无法找出错误。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM