[英]Using AJAX Post and Node.js Express
I am trying to send data from my html for node.js Express using a post method. 我正在尝试使用post方法从HTML发送用于node.js Express的数据。
Using this code on my html file: 在我的html文件上使用以下代码:
function readfile() {
var data = {};
data.path = '/home/test/pgadmin.txt';
data.ext = '.txt';
console.log(data);
$.ajax({
url: '/read_file',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(data) {
console.log(data);
}
});
}
And this is the code that I'm using on server side. 这是我在服务器端使用的代码。
var express = require('express')
var path = require('path')
var app = express()
app.post('/read_file', function(req, res) {
console.log(req.data.path) //?
console.log(req.data.ext) //?
//I dont know how to get the values of my data: here
})
Is there some way to get those data values without using bodyparser? 有什么方法可以在不使用bodyparser的情况下获取那些数据值?
I'm not sure why you don't want to use bodyParser
, but this could be done like: 我不确定您为什么不想使用bodyParser
,但是可以这样做:
var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/read_file', function(req, res) {
console.log(req.body);
});
Of course you have to install bodyParser
npm module as Brian pointed out. 当然,必须像Brian指出的那样安装bodyParser
npm模块。
See How do I consume the JSON POST data in an Express application 请参阅如何在Express应用程序中使用JSON POST数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.