2012-09-27 101 views
0

我有以下的JSON,我从一个URL收到,我们称这里的URL www.blabla.com/testjson的JavaScript返回undefined

,并返回:

[{"Identifier":1,"Naam":"NOT Given","Adres":"Kopenhagen 9","Postcode":"0000LL","Plaats":"NOT Given","Longitude":"0.00000","Latitude":"0.00000"}, 
    {"Identifier":2,"Naam":"NOT Given","Adres":"NOT Given 1","Postcode":"0000LL","Plaats":"Rotterdam","Longitude":"0.00000","Latitude":"0.00000"}, 
    {"Identifier":3,"Naam":"NOT Given","Adres":"NOT Given 6","Postcode":"0000LL","Plaats":"Rotterdam","Longitude":"0.00000","Latitude":"0.00000"}, 
    {"Identifier":4,"Naam":"NOT Given","Adres":"NOT Given 1","Postcode":"0000LL","Plaats":"Den Haag","Longitude":"0.00000","Latitude":"0.00000"}, 
    {"Identifier":5,"Naam":"NOT Given","Adres":"NOT Given 218","Postcode":"0000LL","Plaats":"Zoetermeer","Longitude":"0.00000","Latitude":"0.00000"} 
]; 

我想取出这个JSON带回到这个结果,但我得到了一个未定义,这是我的代码:

$.each("www.blabla.com/testjson", function(index, element) { 
    $('.result').append(element.Naam); 
    alert(element.Naam); 
}); 

你还需要检查用户的cookie是真实的,否则它不会返回的东西,因为我用的pH工作,我不这样做onegap和jquery mobile。这可能是问题吗?

+1

在这种情况下,我想,'$ .each'将迭代字符串的每个字符。并且字符串没有“Naam”属性(这就是为什么你得到'undefined')。它不会执行AJAX请求。请再次查看文档:http://api.jquery.com/category/ajax/,http://api.jquery.com/jQuery.each/。 –

+0

你能提供你试图用来获取json文件的url吗? –

回答

3

尝试使用方法$.getJSON

$.getJSON('http://www.blabla.com/testjson.json', function(data) { 
    $.each(data, function(key, val) { 
     $('.result').append(val.Naam); 
     alert(val.Naam); 
    }); 
}); 

欲了解更多信息,请在线文档:http://api.jquery.com/jQuery.getJSON/

PS:确保你的网站www.blabla.com添加到您的白名单例外。

有关PhoneGap的白名单中的异常详细信息,请在线文档(以下链接是的PhoneGap /科尔多瓦版本2.1.0):http://docs.phonegap.com/en/2.1.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide

+0

做到这一点,现在我什么都没有回来,是因为我需要设置饼干? – bdz

+0

您是否将网站www.blabla.com添加到白名单例外? – Littm

+0

@bdz:它与cookies无关。如果网址是外部网址,您可能会遇到与**相同来源的政策**:http://en.wikipedia.org/wiki/Same_origin_policy问题。 –

1

您的代码将遍历帕拉姆字符串“WWW的每个字符。 blabla.com/testjson“,返回'未定义',因为通过写入element.Naam你试图访问一个名字为'Naam'的字符串的不存在的属性。

您需要通过使用ajax调用来加载外部json内容并解析它。

这里是Littm的答案,如果你有兴趣的错误处理过较长的选择:

$(document).ready(function() 
    { 
     $.ajax(
     { 
      type: "GET", 
      url: "data.json", 
      data: "nocache=" + Math.random(), 

      // dataType: "json", 
      // the above requires 'application/json' as Content-Type headers 
      // see 'reference A' in the footer notes 
      // if you active this, please modify following code accordingly 

      success: function (source, textStatus, jqXHR) 
      { 
       var data = jQuery.parseJSON(source); 
       $.each(data, function(index, value) 
       { 
        alert(index+" "+value.Naam); 
       }); 
      }, 

      error: function(data) 
      { 
       // alert("ERROR"); 
      } 
     }); 
    }); 

注:
1. Reference A

编辑: 要获得更多错误信息,请添加以下内容:

$(document).ajaxError(
    function (event, jqXHR, ajaxSettings, thrownError) 
    { 
     console.log(event); 
     console.log(jqXHR); 
     console.log(ajaxSettings); 
     console.log(thrownError); 
    }); 

然后使用chrome控制台。

编辑:
如果键入您在浏览器中提供的网址,我重定向到一个登录页面。
我认为你的ajax代码也得到这个HTML文档。

尝试检查你的会话:你可以在你的代码中添加一个div,并使用curl在这个新div中加载你的url的内容。直到你没有在这个div里面看到你的json代码,你的会话变量才能正常工作。
如果curl能正确地获得你的文件内容,那么就会做ajax。

编辑:
请参阅?你的问题是要正确登录到你试图获取json文件的web服务。

你的朋友是对的。
你必须在PHP中正确设置你的$_SESSION,它基本上使用cookies来保留会话信息。

尝试正确:

​​

在你的PHP代码开始,用ajax之前。
参考文献:
- php.net session_start
- PHP session not working with JQuery Ajax?


请注意,这只会在同一个域中工作:
Why jquery ajax not sending session cookie

如果您正在使用不同的子域,你可以尝试使用

setcookie 

参考文献: - php.net setcookie
- Cross domain cookie access (or session)

据我知道有没有办法来设置不同的领域内的cookie。
如果你在这种情况下,我建议你看看SimpleSAMLPHP descbribed这里: cross domain cookies


祝你好运:)

+0

不返回任何内容:( – bdz

+0

@bdz请参阅我的编辑 –

+0

f。事件 currentTarget当前:HTMLDocument的 数据:未定义 delegateTarget:HTMLDocument的 独家:未定义 handleObj:对象 isTrigger:真 jQuery17109108295564074069:真 命名空间: “” namespace_re:空 结果:未定义 目标:HTMLDocument的 时间戳:1348756349438 类型:“ajaxError” __proto__:对象 – bdz