2012-09-27 103 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