3

我很感兴趣谷歌如何编码 POST 参数。在一个应用程序中,我发现了以下方法,假设我有以下对象:

selection={"ty":"mc","cl":{"loc_type":0,"si":9,"aps":false},"sr":[]}

在 POST 请求中,它采用以下形式:

 selection=%7B%22ty%22%3A%22mc%22%2C%22cl%22%3A%7B%22loc_type%22%3A0%2C%22si%22%3A9%2C%22aps%22%3Afalse%7D%2C%22sr%22%3A%5B%5D%7D

这里应用了哪种方法?

4

4 回答 4

1

encodeURIComponent使用和JSON.stringify函数可以达到同样的效果:

"selection=" + encodeURIComponent(JSON.stringify(selection))
于 2012-05-31T11:21:27.037 回答
0

它被称为 URL 编码。

它将非 ASCII 字符和在 URI 方案中具有特殊含义的字符替换为 ASCII 表示:每个不可打印的字符将被写入该字符的 ASCII 表中的索引%xy位置。xy

有许多开箱即用的编程语言支持它:

  • 在 JavaScript 中,您可以使用 encodeURIComponent() 或 encodeURI() 函数。

您可以像这样轻松调用它,例如:

var myjson = '{my:json}';
url_encoded_json = encodeURIComponent( myjson );
alert(url_encoded_json);

在其他语言中:

  • PHP 有 rawurlencode() 函数。
  • ASP 具有 Server.URLEncode() 函数。
  • Python 有 urllib.urlencode() 函数。
  • Java 有 java.net.URI(url).toASCIIString() 函数
于 2012-05-31T11:16:16.643 回答
0

它只是 URL 编码

查看内置函数 encodeURIComponent(str) 和 encodeURI(str)

于 2012-05-31T11:19:12.493 回答
0

javascript中的方法称为encodeURIComponent()write

alert(encodeURIComponent('{"ty":"mc","cl":{"loc_type":0,"si":9,"aps":false},"sr":[]}'));
于 2012-05-31T11:20:58.710 回答