从前端JS解析变量值到NodeServe.JS文件
Parse variable value from front end JS to the Node Serve.JS file
如何将前端 JS 的变量 skip 值传递给 Node Serve.JS 文件?目前这些是我拥有的代码。知道我哪里写错了吗?
Server.js (NodeJs)
app.get("/api/routes", function(req , res){
// Configure the request
const skip = req.query.skip;
var options = {
url: 'http://dataservice/trainroutes?$skip='+skip,
method: 'GET',
encoding: null,
headers: {
AccountKey: 'XXX',
accept: 'application/json'
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.writeHead(200, response.headers);
res.write(body);
res.end();
}
else {
res.send("Error");
}
})
});
前端JS:
function busseries(){
var skip = 14000;
$.ajax({
url: "/api/routes?skip="+skip,
success: function(results5) {
//Set result to a variable for writing
var objs5 = JSON.stringify(results5);
var routetimeobjs5 = JSON.parse(objs5);
console.log(routetimeobjs5)
}
});
}
像这样用模板文字传递它。
url: `/api/routes?skip=${skip}`
如何将前端 JS 的变量 skip 值传递给 Node Serve.JS 文件?目前这些是我拥有的代码。知道我哪里写错了吗?
Server.js (NodeJs)
app.get("/api/routes", function(req , res){
// Configure the request
const skip = req.query.skip;
var options = {
url: 'http://dataservice/trainroutes?$skip='+skip,
method: 'GET',
encoding: null,
headers: {
AccountKey: 'XXX',
accept: 'application/json'
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.writeHead(200, response.headers);
res.write(body);
res.end();
}
else {
res.send("Error");
}
})
});
前端JS:
function busseries(){
var skip = 14000;
$.ajax({
url: "/api/routes?skip="+skip,
success: function(results5) {
//Set result to a variable for writing
var objs5 = JSON.stringify(results5);
var routetimeobjs5 = JSON.parse(objs5);
console.log(routetimeobjs5)
}
});
}
像这样用模板文字传递它。
url: `/api/routes?skip=${skip}`