json在IE中导致问题

时间:2011-03-01 20:47:43

标签: javascript json internet-explorer

我正在使用这个带有json字符串的javascript来将数据发送到flash项目

        var flashvars = { 
        xmlFile: 'http://iyt.psu.edu/xml/abington/home.xml',
        preface: 'http://iyt.psu.edu/',
        preload: '{"url":"flash/project.swf","x":"375","y":"237","link":"home","tween":{"prop":"y","begin":"0","finish":"-200","duration":"1"}}'
    };

然而预载线导致IE中的问题任何人都知道除了使用IE之外我可能做错了什么; ^)

3 个答案:

答案 0 :(得分:2)

如果有一个尾随逗号并且您使用的是FireFox或基于Webkit的浏览器,那么一切都会正常。但是在IE中,任何没有对象属性的尾随逗号都会导致一个可能不那么明显的问题。

这会失败。最后看到额外的逗号:

var flashvars = { 
        "xmlFile" : "http://iyt.psu.edu/xml/abington/home.xml",
        "preface" : "http://iyt.psu.edu/",
        "preload" : "{'url': 'flash/project.swf' , 'x': '375 ', 'y': '237', 'link': 'home', 'tween' : {'prop':'y','begin' : '0', 'finish' : '-200' , 'duration' : '1' }}",
}

通过http://www.jslint.com/测试的格式正确的JSON永远不会受到伤害。

var flashvars = { 
        "xmlFile" : "http://iyt.psu.edu/xml/abington/home.xml",
        "preface" : "http://iyt.psu.edu/",
        "preload" : "{'url': 'flash/project.swf' , 'x': '375 ', 'y': '237', 'link': 'home', 'tween' : {'prop':'y','begin' : '0', 'finish' : '-200' , 'duration' : '1' }}"
}

但是你粘贴的JSON看起来还不错。此外,也许Word文档中包含撇号。

答案 1 :(得分:0)

可能需要在JSON字符串中转义/

答案 2 :(得分:0)

实际上你应该使用服务器端或javascript工具对json字符串进行编码,然后AS3会自动解码它:

例如在JSP中:

var flashvars = { 
        xmlFile: 'http://iyt.psu.edu/xml/abington/home.xml',
        preface: 'http://iyt.psu.edu/',
        preload: '<c:out value="{"url":"flash/project.swf","x":"375","y":"237","link":"home","tween":{"prop":"y","begin":"0","finish":"-200","duration":"1"}}'" />'
    };