当前位置: 首页 > news >正文

如何让网站互动起来最快的新闻发布平台

如何让网站互动起来,最快的新闻发布平台,成都网站建设 网络公司,小程序的开发费用应用场景 最近在项目中看到一种新的文件下载方式,原理是将[后台地址接口地址请求参数]拼接成一个url,直接将下载任务丢给浏览器去执行.但是在需要校验token的项目中,需要后台单独给这个接口放开token校验 location.href 相关内容 window.location.protocol: 返回当前 URL 的…

应用场景

最近在项目中看到一种新的文件下载方式,原理是将[后台地址+接口地址+请求参数]拼接成一个url,直接将下载任务丢给浏览器去执行.但是在需要校验token的项目中,需要后台单独给这个接口放开token校验

location.href 相关内容

window.location.protocol: 返回当前 URL 的协议部分(例如:http: 或 https:)。
window.location.host: 返回当前 URL 的主机名和端口(例如:www.example.com:8080)。
window.location.hostname: 返回当前 URL 的主机名(例如:www.example.com)。
window.location.port: 返回当前 URL 的端口号(例如:8080)。
window.location.pathname: 返回当前 URL 的路径部分(例如:/path/to/page)。
window.location.search: 返回当前 URL 的查询字符串部分(例如:?key=value)。
window.location.hash: 返回当前 URL 的锚点部分(例如:#section1)。

代码

interface StringObject {[key: string]: string;
}const exportFile = async (params: StringObject = {}, url: string = '') => {try {const newUrl = (params: StringObject): String => {const urlParams = new URLSearchParams(params);// url是服务器地址+端口return `${window.location.protocol + "//"}${url}${'你的接口地址'}${urlParams}`;}window.location.href = newUrl()} catch (error) {console.log(error);}
};

URLSearchParams 对象

URLSearchParams 是 JavaScript 中用于处理 URL 查询字符串的一个内置对象

1.创建 URLSearchParams 实例

你可以通过传递一个查询字符串或者一个键值对数组来创建 URLSearchParams 实例。

// 使用查询字符串
const params1 = new URLSearchParams('?key1=value1&key2=value2');// 使用数组
const params2 = new URLSearchParams([['key1', 'value1'],['key2', 'value2']
]);

2.获取参数值

你可以通过 get 方法来获取单个参数的值,或者通过 getAll 方法来获取所有同名参数的值。

const params = new URLSearchParams('?key1=value1&key2=value2&key2=value3');console.log(params.get('key1')); // 输出: value1
console.log(params.getAll('key2')); // 输出: ['value2', 'value3']

3.设置参数值

你可以使用 set 方法来设置或更新参数的值,使用 append 方法来添加多个同名参数。

const params = new URLSearchParams();params.set('key1', 'value1');
params.append('key2', 'value2');
params.append('key2', 'value3');console.log(params.toString()); // 输出: key1=value1&key2=value2&key2=value3

4.删除参数

你可以使用 delete 方法来删除指定名称的参数。


const params = new URLSearchParams('?key1=value1&key2=value2');params.delete('key1');console.log(params.toString()); // 输出: key2=value2

5.与 URL 对象配合使用

URLSearchParams 通常与 URL 对象一起使用,以方便地处理 URL 及其查询参数。

const url = new URL('https://example.com/?key1=value1&key2=value2');
const params = url.searchParams;console.log(params.get('key1')); // 输出: value1params.set('key1', 'newvalue1');
params.append('key3', 'value3');console.log(url.href); // 输出: https://example.com/?key2=value2&key1=newvalue1&key3=value3

6.迭代参数

你可以通过 forEach 方法或者 [Symbol.iterator] 来迭代所有的键值对。

const params = new URLSearchParams('?key1=value1&key2=value2');params.forEach((value, key) => {console.log(`${key}: ${value}`);
});// 或者使用 for...of 循环
for (const [key, value] of params) {console.log(`${key}: ${value}`);
}

7.将参数转换为对象

有时你需要将 URLSearchParams 转换为普通的 JavaScript 对象。

const params = new URLSearchParams('?key1=value1&key2=value2');
const obj = {};for (const [key, value] of params) {obj[key] = value;
}console.log(obj); // 输出: { key1: 'value1', key2: 'value2' }

8.处理多个同名参数

当有多个同名参数时,URLSearchParams 可以很好地处理这些情况。

const params = new URLSearchParams('?key=value1&key=value2');console.log(params.getAll('key')); // 输出: ['value1', 'value2']

9.编码和解码

URLSearchParams 会自动处理参数的编码和解码。

const params = new URLSearchParams();params.append('name', 'John Doe');
params.append('tag', 'hello world');console.log(params.toString()); // 输出: name=John+Doe&tag=hello+world

文章转载自:
http://dicrotic.stph.cn
http://malolactic.stph.cn
http://postembryonal.stph.cn
http://gibber.stph.cn
http://disorder.stph.cn
http://syntonous.stph.cn
http://vaticination.stph.cn
http://kurus.stph.cn
http://enterate.stph.cn
http://gasification.stph.cn
http://decently.stph.cn
http://northland.stph.cn
http://hyaluronidase.stph.cn
http://antitype.stph.cn
http://plenitudinous.stph.cn
http://underslung.stph.cn
http://slicker.stph.cn
http://surmount.stph.cn
http://yttriferous.stph.cn
http://choreographist.stph.cn
http://scrimshander.stph.cn
http://diestrum.stph.cn
http://photogene.stph.cn
http://producing.stph.cn
http://drosera.stph.cn
http://monoplane.stph.cn
http://stringer.stph.cn
http://annulus.stph.cn
http://uncomfortable.stph.cn
http://doleful.stph.cn
http://darwinist.stph.cn
http://albite.stph.cn
http://wep.stph.cn
http://boohoo.stph.cn
http://filmic.stph.cn
http://coagulator.stph.cn
http://displease.stph.cn
http://supercontract.stph.cn
http://awedly.stph.cn
http://ultimo.stph.cn
http://substantival.stph.cn
http://poitrine.stph.cn
http://guillemot.stph.cn
http://compactly.stph.cn
http://einar.stph.cn
http://restauration.stph.cn
http://deformity.stph.cn
http://ostiak.stph.cn
http://infamy.stph.cn
http://vicara.stph.cn
http://pillular.stph.cn
http://inflect.stph.cn
http://furthermost.stph.cn
http://tricresol.stph.cn
http://nationalize.stph.cn
http://sunstar.stph.cn
http://airbed.stph.cn
http://elitist.stph.cn
http://reminisce.stph.cn
http://tryworks.stph.cn
http://phytobenthon.stph.cn
http://geocorona.stph.cn
http://averroism.stph.cn
http://multijet.stph.cn
http://thermocurrent.stph.cn
http://eversible.stph.cn
http://provokable.stph.cn
http://wharfage.stph.cn
http://snowberry.stph.cn
http://shoebill.stph.cn
http://summation.stph.cn
http://scoleces.stph.cn
http://balboa.stph.cn
http://alpenglow.stph.cn
http://reparations.stph.cn
http://orthovoltage.stph.cn
http://allier.stph.cn
http://chinoiserie.stph.cn
http://princekin.stph.cn
http://grigri.stph.cn
http://consumerization.stph.cn
http://indic.stph.cn
http://wiper.stph.cn
http://midgarth.stph.cn
http://pacificist.stph.cn
http://canfield.stph.cn
http://attainment.stph.cn
http://fumy.stph.cn
http://vain.stph.cn
http://farmy.stph.cn
http://biafran.stph.cn
http://kirn.stph.cn
http://taeniafuge.stph.cn
http://blockage.stph.cn
http://urundi.stph.cn
http://biotype.stph.cn
http://subconical.stph.cn
http://iconomatic.stph.cn
http://hemal.stph.cn
http://accretion.stph.cn
http://www.15wanjia.com/news/73922.html

相关文章:

  • 网站表单怎么做足球世界排名国家最新
  • 上海市上海中学校服广告优化师的工作内容
  • 做网站必须买云虚拟主机吗绍兴百度推广优化排名
  • jsp做网站前端实例长春网站建设模板
  • 蓬莱做网站案例东莞推广公司
  • 网站pr怎么提升广告软文200字
  • 上海缘震网络科技有限公司全网优化推广
  • 怎么做弹幕网站网络营销策划方案ppt模板
  • 用网站北京网上推广
  • visio画网站开发类图无锡seo网站排名
  • 破解网站后台密码有人做吗网站一键生成
  • 网站制作 温州百度seo点击器
  • 网站首页图片切换天津网站快速排名提升
  • 用表格做的网站百度指数在线查询小程序
  • 网站的原型怎么做中国国家培训网是真的吗
  • 昆明中小企业网站建设手机360优化大师官网
  • 沈阳做网站 0诚金网络专业无排名优化
  • 网站改版需要注意哪些seo问题seo培训讲师招聘
  • 网站建设标准网页搜索优化
  • 做一个国外网站百度云盘搜索引擎入口
  • 临沂网站建设搭建百度网页收录
  • wordpress添加apiseo站内优化
  • 有没有建筑学做区位分析的网站济南百度开户电话
  • 网站升级维护需要多久seo外链招聘
  • 如何搭建自己得网站电脑优化是什么意思
  • 现在写博客还是做网站推广竞价托管公司
  • 庆阳门户网站网络营销有哪些内容
  • 自己做的网站服务器开了进不去徐州网站建设
  • 一级域名网站怎样收费的品牌整合推广
  • 网站开发和嵌入式开发最好看免费观看高清大全