URLSearchParams为第一个查询字符串返回null [英] URLSearchParams returning null for the first query string

查看:37
本文介绍了URLSearchParams为第一个查询字符串返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么时间段为空

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

但是在下面的代码中它可以工作

But in the below code it works

  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检索参数:

You are supposed to create an URL object and then retrieve the params with url.search : 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')}`);

这篇关于URLSearchParams为第一个查询字符串返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆