URLSearchParams 对于第一个查询字符串返回 null

2024-04-11

为什么时间段为空

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 https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams#Examples

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')}`);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

URLSearchParams 对于第一个查询字符串返回 null 的相关文章

随机推荐