简体   繁体   English

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

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

I'm trying to send data using vue.js to my node.js server, but the browser console keeps showing me a 404: POST http://127.0.0.1:63342/myaction 404 (Not Found) 我正在尝试使用vue.js将数据发送到我的node.js服务器,但是浏览器控制台不断向我显示404: POST http://127.0.0.1:63342/myaction 404 (Not Found)

vue.js: vue.js:

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

node.js: 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());
});

When I start my server, it says it's running at { address: '::', family: 'IPv6', port: 63342 } 当我启动服务器时,它说它正在{ address: '::', family: 'IPv6', port: 63342 }

The POST worked without vue.js, by simply submitting a HTML form , but now AJAX doesn't wordk. POST无需vue.js,只需提交HTML form ,但现在AJAX不再使用。 I tried multiple ports and folders, but can't figure out the mistake. 我尝试了多个端口和文件夹,但无法找出错误。

I finally figured it out. 我终于弄明白了。 You have to manually add an address to the listener: 您必须手动向侦听器添加地址:

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

And in my case I was using the same port for API and Frontend, I had to switch ports and allow CORS 在我的情况下,我为API和前端使用了相同的端口,因此我不得不切换端口并允许CORS

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

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