gpt4 book ai didi

javascript - URLSearchParams 为第一个查询字符串返回 null

转载 作者:行者123 更新时间:2023-11-28 00:38:09 25 4
gpt4 key购买 nike

为什么timeperiod为空

const url = 'http://example.com?timeperiod=lasttwentyeightdays&pagesize=20'
const args = new URLSearchParams(url);
alert('timeperiod='+args.get('timeperiod') + ' and pagesize='+ args.get('pagesize'));

但在下面的代码中它有效

  const url = 'http://example.com?x=c&timeperiod=lasttwentyeightdays&pagesize=20'
const args = new URLSearchParams(url);
alert('timeperiod='+args.get('timeperiod') + ' and pagesize='+ args.get('pagesize'));

最佳答案

您需要创建一个 URL 对象,然后使用 url.search 检索参数:

See

const url = new URL('http://example.com?timeperiod=lasttwentyeightdays&pagesize=20');

const args = new URLSearchParams(url.search);

console.log(`timeperiod=${args.get('timeperiod')} and pagesize=${ args.get('pagesize')}`);

关于javascript - URLSearchParams 为第一个查询字符串返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103757/

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