现在的位置: 首页 > 综合 > 正文

页面跳转字符串转换为Json格式

2013年07月30日 ⁄ 综合 ⁄ 共 719字 ⁄ 字号 评论关闭

在做一个网站,一个页面通过地址传值到另外一个页面,接收页面得到值后需要转换为Json对象的数据。
因为有用Jquery所以一开始用了Jquery中的一个方法

var Info = jQuery.parseJSON(Request("Info"));

在IE浏览器下,网页正常运行,但是在火狐浏览器,google浏览器中,会出现错误。
只好使用JS的方法;尝试了两种方法:

1:var Info = eval("(" + decodeURIComponent(Request("Info")) + ")");//注意需要decodeURIComponent()方法进行编码转换
2:var Info = (new Function("", "return " + decodeURIComponent(Request("Info"))))();//用这个方法的人越来越少了,//注意也需要decodeURIComponent()方法进行编码转换

注意:
decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

<script type="text/javascript">

var test1="http://www.w3school.com.cn/My first/"

document.write(encodeURIComponent(test1)+ "<br />")
document.write(decodeURIComponent(test1))

</script>

 

输出:

http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
http://www.w3school.com.cn/My first/

抱歉!评论已关闭.