Chrome和Firefox为json提供不同的结果(键入结果)

时间:2012-08-31 14:32:22

标签: jquery json firefox google-chrome

我有这段代码:

$.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:“英格兰”   generation_high:18   generation_low:1   id:8258   名称:“温布利中心”   parent_area:2488   类型:“LBW”   type_name:“伦敦自治市区”    proto :对象

但是,在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行)

ë

index_fb.js(第48行)

名词

index_fb.js(第48行)

index_fb.js(第48行)

_

index_fb.js(第48行)

index_fb.js(第48行)

[R

index_fb.js(第48行)

ë

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(响应)是正确的,但它似乎占用了:

中的每一个字母
for (var k in response) {

k = firefox中的数字,与chrome一样,它将k作为整数8258处理。

我如何解决这个问题,以便两者兼容?

由于

修改

这是新代码:仍然是同一个问题:

$.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用于非安全源时存在问题。

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

1 个答案:

答案 0 :(得分:2)

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

的最终参数设置dataType
$.get(url,successFunction,"json")
相关问题