簡體   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