我想将以AJAX jquery编写的函数的参数(source_lead)传递给以节点js编写的服务器部分的另一个函数。 我该怎么做 ?
AJAX jQuery:
$("#importdata").on('click', function () {
var source_lead = $("#source").val();
$.get("/import", function (data) {
$("#message").show().html(data['success']);
});
});
Node JS:
}).get('/import', function (req, res, next) {
// var source_lead = fs.readFileSync('source.txt', 'utf-8');
var csvfile = __dirname + "/../public/Tables/leads.csv";
var stream = fs.createReadStream(csvfile);
var products = [];
var csvStream = csv()
.on("data", function (data) {
var item = new Product({
name: data[13],
age: data[5],
phone: data[14],
email: data[15],
date: data[1],
// source: source_lead
});
item.save(function (error) {
console.log(item);
if (error) {
throw error;
}
});
}).on("end", function () {
});
stream.pipe(csvStream);
res.json({success: "Data imported successfully.", status: 200});
})
答案 0 :(得分:0)
1)客户端
$("#importdata").on('click', function () {
var source_lead = $("#source").val();
$.get("/import?source_lead=json", function (data) {
$("#message").show().html(data['success']);
});
});
2)NodeJ ....
}).get('/import', function (req, res, next) {
let source_lead = req.query.source_lead
})
答案 1 :(得分:0)
您可以通过ajax调用传递query params
并将其接收到nodejs api get
端点。
JavaScript
$.get("/import?data=myData", function (data) {
$("#message").show().html(data['success']);
});
NodeJS
}).get('/import', function (req, res, next) {
var data = req.query.data; // Query param data
....