gpt4 book ai didi

javascript - 如何从url获取参数到变量

转载 作者:搜寻专家 更新时间:2023-10-31 23:39:47 25 4
gpt4 key购买 nike

我正在使用 express 。当我请求带有参数的页面时,我想获取 json。即

当我输入:http://localhost:3000/GetItemStock?fitemid=1005

我收到错误消息,显示 strItemID 未定义。请解决这个问题。以下是 node.js 代码。

  router.get('/GetItemStock', function(req, res, next) {
var url = require('url');
var url_parts = url.parse(req.url, true);
var param = url_parts.query;
var strItemID = req.param.fitemid; // $_GET["id"]

GetItemStock(function(recordset)
{

res.send(recordset);
});
});

function GetItemStock(strItemID,callback)
{
var sql = require('mssql');
var config ={
user: 'sa',
password: '123456789',
database: 'ROYAL',
host: '127.0.0.1,1433'
};
var cn = new sql.Connection(config,function(err)
{

var str1 = "SELECT tblBranch.fBrName, (sum(tblstocktrans.fqtyin)-sum(tblstocktrans.fqtyout)) from tblStockTrans ,tblbranch where (tblStockTrans.fItemID = '"
var str2 ="') and (tblBranch.fbrid=tblStockTrans.fbrid) group by fbrname order by fitemid"
var strQuery = str1 + strItemID + str2
var request = new sql.Request(cn)
request.query(strQuery,function(err,recordset/* data*/)
{
callback(recordset);
});
});
}
module.exports = router;

这是我遇到的错误。请帮忙

**c:\api\RmaxErpAPI\routes\index.js:117
var strQuery = str1 + strItemID + str2
^

ReferenceError: strItemID 未定义**

最佳答案

试试这样吧,因为它是一个查询字符串。

var strItemID = req.query.fitemid;

关于javascript - 如何从url获取参数到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com