简体   繁体   English

encodeURI(JSON.stringify()) 在 URL 中显示 %255B

[英]encodeURI(JSON.stringify()) showing %255B in URL

I am trying to pass along a queryParam in Angular that consists of an array of objects as such fooArray = [{foo: 'bar', foo: false}, {foo: 'bar', foo: false}] .我试图在 Angular 中传递一个 queryParam,它由一个对象数组组成,例如fooArray = [{foo: 'bar', foo: false}, {foo: 'bar', foo: false}] In the URL queryParam I am receiving the following: %255B when using encodeURI(JSON.stringify(this.fooArray))在 URL queryParam 中,我收到以下信息: %255B when using encodeURI(JSON.stringify(this.fooArray))

I have tried using encodeURI(JSON.stringify()) to encode the array into the queryParam and JSON.parse(decodeURIComponent()) to retrieve the param我尝试使用 encodeURI(JSON.stringify()) 将数组编码为 queryParam 和 JSON.parse(decodeURIComponent()) 来检索参数

fooArray = [{foo: 'bar', foo: false}, {foo: 'bar', foo: false}]

fooParam: encodeURI(JSON.stringify(this.fooArray))

JSON.parse(decodeURIComponent(params["fooParam"]))

You're double-encoding the URL.您正在对 URL 进行双重编码。 This:这个:

encodeURI(JSON.stringify([{foo: 'bar', foo: false}, {foo: 'bar', foo: false}]))

results in结果是

"%5B%7B%22foo%22:false%7D,%7B%22foo%22:false%7D%5D"

If you call encodeURI on it again, you get如果你再次调用encodeURI ,你会得到

"%255B%257B%2522foo%2522:false%257D,%257B%2522foo%2522:false%257D%255D"

...because % is encoded as %25 . ...因为%被编码为%25

Two notes:两个注意事项:

  1. You should encode it only once.您应该只对其进行一次编码。 Perhaps you're passing the string to something that will already URI-encode it for you?也许您正在将字符串传递给已经为您进行 URI 编码的东西? If so, don't use encodeURI .如果是这样,请不要使用encodeURI

  2. Usually, you want encodeURIComponent , not encodeURI (because usually you're encoding just a component of hte URI, not the entire thing).通常,您需要encodeURIComponent ,而不是encodeURI (因为通常您只编码 hte URI 的一个组件,而不是整个事物)。

I was running into this problem, and what I discovered was that somewhere along the chain of my code, I was running .toString() , which runs a encodeURI on my string/url.我遇到了这个问题,我发现在我的代码链的某个地方,我正在运行.toString() ,它在我的字符串/url 上运行一个encodeURI

I was essentially double encoding my search params without knowing it.我基本上是在不知情的情况下对我的搜索参数进行双重编码。 If you're running .toString() on an instance of URL or URLSearchParams class, then you don't need to manually run encodeURI or encodeURIComponent如果您在URLURLSearchParams class 的实例上运行.toString() ,则无需手动运行encodeURIencodeURIComponent

Testing For Double Encoding测试双重编码

A quick way to learn if you're double encoding a string is to setup a quick example test run in your javascript developer tools.了解您是否对字符串进行双重编码的一种快速方法是在 javascript 开发人员工具中设置一个快速示例测试运行。 Visualizing this help me a lot:可视化这对我有很大帮助:

const searchParams1 = '?status=open&created_by=bobby&date';
const url1 = "http://example.com/" + searchParams1;
const searchParamsString1 = new URL(exampl1).toString() // NOTE: `toString()` runs encode on the string for you!

const searchParams2 = '?status=open&date_filter={"by":"created","qualifier":"between","value":["2022/04/01","2022/04/02"]}';
const example2 = "http://example.com/" + searchParams2;
const searchParamsString2 = new URL(example2).toString();

// encoded 1x
searchParamsString2 ===
  "http://example.com/?status=open&date_filter={%22by%22:%22created%22,%22qualifier%22:%22between%22,%22value%22:[%222022/04/01%22,%222022/04/02%22]}";

// encoded 2x (visual)
const example2DoubleEncoded = encodeURI(
  "http://example.com/?status=open&date_filter={%22by%22:%22created%22,%22qualifier%22:%22between%22,%22value%22:[%222022/04/01%22,%222022/04/02%22]}"
);
example2DoubleEncoded ==="http://example.com/?status=open&date_filter=%7B%2522by%2522:%2522created%2522,%2522qualifier%2522:%2522between%2522,%2522value%2522:%5B%25222022/04/01%2522,%25222022/04/02%2522%5D%7D";



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM