为什么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
检索参数:
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/