簡體   English   中英

在node.js中獲取jQuery發布數據

[英]get jquery post data in node.js

我希望這不是一個愚蠢的問題,也不是重復的問題,但似乎在我看來到處都存在一個不同的答案,而我卻無法讓他們中的任何一個起作用。

使用jQuery和PHP,我可以執行以下操作來在前端和后端之間發送和接收數據->

$.post("logic.php", {
    command: command
}, function(data, status) {
    $("#output").html(data);
}); //jQuery (front-end)


if (isset($_POST['command'])) {
    $command = $_POST['command'];
    if ($command == 'about') {
        echo 'about';
    } else {
        echo 'command not found';
    }
} // PHP (back-end)

通訊有效並且看起來很簡單。 現在,我正在轉換為Node.js,並且很難獲得相同的效果。 我沒有收到任何錯誤,但似乎根本沒有交流...我嘗試了許多不同的方法,但是這就是我現在所擁有的->

$.post("index.js", {
    command: command
}, function(data, status) {
    out(data); /* out is a function i made that puts the data on the page in a nice format */
}); // jQuery (front-end)


var express = require('express');
var bodyParser = require('body-parser')
var app = express();
// Javascript back-end
app.post('/', function (req, res, next) {
    console.log(req.body) ;
}); // Should log command to the console but nothing at all happens

我是node.js的新手,但想從PHP過渡,因為我聽說了強大的AJAX功能(這是我的應用程序所基於的功能)...請幫助:)

您定義了bodyParser ,但是從不使用它。 您必須將其添加到快速配置中。

app.use(bodyParser.urlencoded());

此后, req.body將不再是undefined

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM