我正在尝试将数据从客户端发送到服务器(节点 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" 。在节点js中接收来自客户端的post请求及其数据的正确方法是什么
使用这个 npm 包 - https://www.npmjs.com/package/body-parser
因此服务器站点解析如下: request.body.{some field name}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句