javascript - 如何从node js post ajax请求中读取数据?

标签 javascript jquery node.js ajax web

我正在尝试将数据从客户端发送到服务器( Node js)。我正在使用 ajax 。

客户端:

$("#test_button").on("click",function(){
    //alert("shit");
    $.ajax(
        {url: "http://localhost:4000/ajax_check", 
        async: false, 
        type: "POST",
        data: "{user:balayya,password:hero}",
        success: function(result){
            alert("hooli");
        }});
});

服务器:

var app = require('express')();
var express = require('express');
var http = require('http').Server(app);


http.listen(process.env.PORT || 4000, function() {
    console.log('listening on *:4000');
});

app.use(express.static('publuc'));

app.get('/', function(req, res) {
    console.log("new entry page serving");
    res.sendFile(__dirname + '/main.html');
});

app.post('/ajax_check', function(req, res){
    console.log("someone came in here");
    console.log(req.query.data);
});

console.log() 打印为“undefined”。 在node js中接收post请求及其来自客户端的数据的正确方法是什么

最佳答案

使用这个 npm 包 - https://www.npmjs.com/package/body-parser

所以服务器站点解析如下: request.body.{某些字段名称}

关于javascript - 如何从node js post ajax请求中读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963891/

相关文章:

JavaScript - include() - 检查数组中是否有多个元素

php - 离线填写表格

jquery获取json数组的长度

jquery - 什么触发了 jQuery Ready() 函数?

javascript - 在 Javascript 中抓取表格元素

node.js - 如何将 .npmignore 目录中的单个文件列入白名单?

node.js - nginx-proxy 用于多个 Node 应用程序 docker

javascript - window.location.href 在 mac safari <a> 上不起作用?

node.js - Node JS 需要 ('path' )

javascript - $.load(content) 之后在 jQuery 中无法访问元素