如何使用JS从URI获取查询参数和编码信息

时间:2019-05-14 11:10:48

标签: javascript node.js npm google-cloud-platform

我在URL后面附加了我的信息,该信息遵循2个查询参数和1个编码信息的格式。是否有任何特定模块可从查询参数获取信息,或者我可能必须使用拆分功能? 样本:?xys = 2929292&abc = 1213123&%5B%7B%22fm%22%3A%2212%22%2C%22id%22%3A%22as-as%22%2C%22pf%22%3A%7B%22nm%22 %3A%22cc%22%7D%2C%22tx%22%3A%7B%22bd%22%3A%22Hi%22%7D%2C%22ts%22%3A%221211%22%2C%22ty%22%3A %22tx%22%2C%22wanm%22%3A%2212123%22%7D%5D

我正在尝试节点服务器

示例:?xys = 2929292&abc = 1213123&%5B%7B%22fm%22%3A%2212%22%2C%22id%22%3A%22as-as%22%2C%22pf%22%3A%7B% 22nm%22%3A%22cc%22%7D%2C%22tx%22%3A%7B%22bd%22%3A%22Hi%22%7D%2C%22ts%22%3A%221211%22%2C%22ty% 22%3A%22tx%22%2C%22wanm%22%3A%2212123%22%7D%5D

预期输出 xys = 2929292 abc = 1213123 message = [{“ fm”:“ 12”,“ id”:“原样”,“ pf”:{“ nm”:“ cc”},“ tx”:{“ bd”:“ Hi”}, “ ts”:“ 1211”,“ ty”:“ tx”,“ wanm”:“ 12123”}]

3 个答案:

答案 0 :(得分:1)

尝试使用 Vanila JS 。像下面

document.location.search

答案 1 :(得分:0)

here

安装query-string软件包并使用它,例如

var const qS = require('query-string');


const parsed = qS.parse(location.search);
console.log(parsed);

答案 2 :(得分:0)

首先您的查询是错误的,应该是

?xys=2929292&abc=1213123&message=%5B%7B%22fm%22%3A%2212%22%2C%22id%22%3A%22as-as%22%2C%22pf%22%3A%7B%22nm%22%3A%22cc%22%7D%2C%22tx%22%3A%7B%22bd%22%3A%22Hi%22%7D%2C%22ts%22%3A%221211%22%2C%22ty%22%3A%22tx%22%2C%22wanm%22%3A%2212123%22%7D%5D

剩下的答案是假设您使用带有express的节点服务器

然后在节点服务器中,您可以从

获取输出
console.log(req.query);
xys = parseFloat(req.query.xys) //remove parseFloat if you want string
abc = parseFloat(req.query.abc)
message = JSON.parse(req.query.message) //JSON.parse() will convert string to array