FF 13,IE 9:JSON stringify /地理定位对象 [英] FF 13, IE 9: JSON stringify / geolocation object
问题描述
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(
function (位置){
//成功处理程序
console.log(position); //输出位置对象到控制台
var gps = JSON.stringify(position);
console.log(gps); //输出一个空字符串!
},
函数(错误)
{
//处理错误
},
{maximumAge:3000,timeout:60000,enableHighAccuracy:true}
);
$ {
//处理错误
}
在Chrome中,输出一个地理位置对象和这个字符串:
{coords:{latitude :XYZ,标题:空,准确度:40,altitudeAccuracy:null,altitude:null,longitude:XYZ,speed:null},timestamp:1339712284200}然而,在Firefox 13中,输出只是一个空字符串,即使打印到控制台的地理位置对象是b所有意图和目的与Chrome显示的对象相同。任何想法在这里出了什么问题? 这个似乎是一个相关的问题,但是我也没有看到解决方案。 IE9显示相同的行为。解决方案发生什么事是JSON.stringify只查看对象的属性默认情况下。
根据DOM规范,所有DOM属性实际上都存在于对象的原型中。
IE和Firefox通过将属性放在原型上来正确执行规范。 Chrome和Safari不会:他们把属性直接放在对象上。这使得这个案例的工作,但打破了其他的事情(例如钩属性获取和设置器的能力)....
有谈论增加到一些DOM的JSON方法对象给JSON.stringify更合理的行为。
I'm trying to get Firefox 13 to turn a geolocation position object into a JSON string, but it's returning an empty string rather than the correct string representation of my JSON object. This is working fine in the latest versions of Chrome and Safari, as well as the Android browser. Here's my code:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
//Success handler
console.log(position); //This outputs the position object to the console
var gps = JSON.stringify(position);
console.log(gps); //This outputs an empty string!
},
function (error)
{
//Handle error
},
{ maximumAge: 3000, timeout: 60000, enableHighAccuracy: true }
);
}
else {
//Handle error
}
In Chrome, this outputs a geolocation object, and this string:
"{"coords":{"latitude":XYZ,"heading":null,"accuracy":40,"altitudeAccuracy":null,"altitude":null,"longitude":XYZ,"speed":null},"timestamp":1339712284200}"
However, in Firefox 13 the output is just an empty string, even though the geolocation object that's printed to the console is to all intents and purposes the same as the object displayed by Chrome. Any ideas on what's going wrong here? This seems to be a related issue, but I don't see a solution there either. IE9 displays the same behaviour, by the way.
解决方案 What's going on is that JSON.stringify only looks at the object's own properties by default.
And per DOM specs all DOM properties actually live on the object's prototype.
IE and Firefox implement the spec correctly by putting the properties on the prototype. Chrome and Safari do not: they put the properties directly on the object. That makes this case work, but breaks other things (e.g. the ability to hook the property getters and setters)....
There's talk of adding toJSON methods to some DOM objects to give them more reasonable behavior for JSON.stringify.
这篇关于FF 13,IE 9:JSON stringify /地理定位对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!