2012-05-31 154 views
3

我很感兴趣如何编码POST params。 在应用程序中的一个,我发现下面的方法,让说我有以下对象:编码json到字符串

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 

哪个应用方法这里?

回答

1

同样的效果可以通过使用来实现和JSON.stringify功能:

"selection=" + encodeURIComponent(JSON.stringify(selection)) 
0

它被称为URL编码。

它使用ASCII表示法替换URI模式中具有特殊含义的非ASCII字符和字符:每个不可打印的字符将被编写为%xy,其中xy是该字符的ASCII表中的索引。

有支持它出的现成的许多编程语言:

  • 在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()函数
+0

'encodeURIComponent'会更合适。 –

0

它只是URL编码

退房内置函数encodeURIComponent方法(STR)与encodeURI(STR)

0

在JavaScript的方法被称为encodeURIComponent()

alert(encodeURIComponent('{"ty":"mc","cl":{"loc_type":0,"si":9,"aps":false},"sr":[]}'));