【问题标题】:Chrome and Firefox give different results json for( key in result)Chrome 和 Firefox 给出不同的结果 json for( key in result)
【发布时间】:2012-08-31 14:32:21
【问题描述】:

我有这个代码:

$.get('http://mapit.mysociety.org/areas/'+ulo, function(response) {

                        console.log(response);

                        var areaList = [];

                        for (var k in response) {
                                 var obj = response[k];
                                 areaList.push(obj);
                                 console.log(response[k]);

                            }
                        var len = areaList.length;

在 Chrome 中,效果很好,例如,将 +ulo 更改为 wembley。

在 Chrome 开发工具上,我根据标题对象下的两个 console.log 获取对象:

对象 index_fb.js:41 [ 目的 all_names:对象 代码:对象 国家:“E” country_name: "英格兰" 世代高:18 世代低:1 编号:8258 名称:“温布利中心” 父区:2488 类型:“LBW” type_name:“伦敦区” 原型:对象

但是,在 Firefox 中也是如此,我在 Firebug 中得到了这个:

{“8258”:{“parent_area”:2488,“generation_high”:18,“all_names”:{},“id”:8258,“codes”:{“ons”:“00AEHE”,“gss” :“E05000104”,“unit_id”:“11458”},“name”:“Wembley Central”,“country”:“E”,“type_name”:“London borough ward”,“generation_low”:1,“country_name” : "英格兰", "类型": "LBW"}}

index_fb.js(第 41 行)

{

index_fb.js(第 48 行)

"

index_fb.js(第 48 行)

8

index_fb.js(第 48 行)

2

index_fb.js(第 48 行)

5

index_fb.js(第 48 行)

8

index_fb.js(第 48 行)

"

index_fb.js(第 48 行)

index_fb.js(第 48 行)

index_fb.js(第 48 行)

{

index_fb.js(第 48 行)

"

index_fb.js(第 48 行)

p

index_fb.js(第 48 行)

一个

index_fb.js(第 48 行)

r

index_fb.js(第 48 行)

e

index_fb.js(第 48 行)

n

index_fb.js(第 48 行)

t

index_fb.js(第 48 行)

_

index_fb.js(第 48 行)

一个

index_fb.js(第 48 行)

r

index_fb.js(第 48 行)

e

index_fb.js(第 48 行)

一个

index_fb.js(第 48 行)

"

index_fb.js(第 48 行)

index_fb.js(第 48 行)

index_fb.js(第 48 行)

2

index_fb.js(第 48 行)

4

index_fb.js(第 48 行)

8

index_fb.js(第 48 行)

8

index_fb.js(第 48 行)

等等等等

所以,Console.log(response) 是正确的,但它似乎包含了以下每个字母:

for (var k in response) {

k = Firefox 中的数字,与 chrome 一样,它将 k 处理为整数 8​​258。

我如何解决这个问题,使其适用于两者?

谢谢

编辑

这是新代码:还是同样的问题:

$.get('http://mapit.mysociety.org/areas/'+ulo, function(response)  {

                        console.log(response);

                        var areaList = [];

                        for (var k in response) {

                            if (response.hasOwnProperty(k)) { 
                                 var obj = response[k];
                                 areaList.push(obj);
                                 }
                            }

我也尝试过,但我需要将 Get 包装到 $.ajax 中以确保它知道返回了 json。该应用在将 ajax 用于非安全源时存在问题。

到目前为止还没有快乐:(

【问题讨论】:

  • 嗨,Rob,抱歉,我不确定我是否理解?
  • 只是一个建议:每当您使用 javascript 执行 for-in 时,将内部部分包裹在 if (obj.hasOwnProperty... 在您的情况下,它将是:for (var k in response) { if ( response.hasOwnProperty(k)) { // 逻辑 } }

标签: jquery json firefox google-chrome


【解决方案1】:

Firefox 认为您的对象是一个字符串。您应该指定 dataType:"json" 以检索正确的 JSON 对象,您将能够枚举哪些属性。 jquery允许你通过$.get的最后一个参数来设置dataType:

$.get(url,successFunction,"json")

【讨论】:

  • 我将如何使用 .get 语句来做到这一点?
  • 我使用了 $.getJSON,当我几周前使用它时,由于 HTTPS 到 HTTP 等原因,它一直给我一个错误。
  • 我在回答中添加了语法,你也可以看看api.jquery.com/jQuery.get
猜你喜欢
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 2016-11-16
  • 2019-03-20
相关资源
最近更新 更多