Firefox中的JSON.stringify错误

时间:2013-12-03 10:55:52

标签: javascript json firefox

我正在为我的应用开发网络分析。

客户端,我通过window.location向服务器发送JSON.stringify(window.location)个对象。

该对象在chrome,IE,opera中正确地进行了字符串化...但在Firefox中,它只返回{"constructor":{}}

Firefox会发生什么?

4 个答案:

答案 0 :(得分:2)

我不知道为什么它在FireFox下不起作用(我认为它与window.location的属性实际上是FireFox中的getter / setter这一事实有关),但这是一个简单的解决方案:只需复制对象。

var copy = {};
for (var i in window.location) {
    copy[i] = window.location[i];
}
JSON.stringify(copy);

答案 1 :(得分:1)

或者,你不能简单地创建一个不是Location API的新对象,而只是一个简单的KVP对象吗?

JSON.stringify({
    href:     window.location.href,
    protocol: window.location.protocol,
    host:     window.location.host,
    hostname: window.location.hostname,
    port:     window.location.port,
    pathname: window.location.pathname,
    search:   window.location.search,
    hash:     window.location.hash,
    username: window.location.username,
    password: window.location.password,
    origin:   window.location.origin
});

答案 2 :(得分:0)

window.locationLocation接口的实例。您可能希望将其url作为字符串。请改为window.location.href

答案 3 :(得分:0)

奇特的解决方案确实有效。

var window_location = {};
for (var i in window.location) {
    window_location[i] = window.location[i];
}
alert(JSON.stringify(window_location));

在行动here中查看此内容...

相关问题