简体   繁体   中英

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:

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());
});

When I start my server, it says it's running at { address: '::', family: 'IPv6', port: 63342 }

The POST worked without vue.js, by simply submitting a HTML form , but now AJAX doesn't wordk. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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