2013-02-07 120 views
0

我似乎無法讓我的jQuery json閱讀器正常工作。我使用下面的代碼(改編自Flickr的例子在jQuery的文檔):jquery getJSON()不工作

<!DOCTYPE html> 
<html> 
<head> 
<title>Test</title> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> 
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> 

</head> 
<body> 
<div data-role="page" id="ScheduleDay"> 
    <div data-role="header"> 
     <a href="#HomePage" data-icon="home" data-direction="reverse">Home</a>  
     <h1 id="ScheduleDayText">Schedule</h1> 
    </div> 
    <div data-role="content"> 
     <ul data-role="listview" id="ScheduleList" data-autodividers="true"> 
     </ul> 
    </div> 
</div> 
<script> 
$(document).on("pageinit", "#ScheduleDay", function(){ 
    $.getJSON("http://oregonffa.com/EventMaster/WebService/ScheduleEvents.php", 
    function(data){ 
     $.each(data, function(i,item){ 
      $("ul#ScheduleList").append('<li time="' + item.StartTime + '"><a href="javascript:viewScheduleEvent(' + item.ScheduleID + ')">' + item.Name + '</a></li>'); 
     }); 
     $("ul#ScheduleList:visible").listview('refresh'); 
    }); 
}); 
</script> 
</body> 
</html> 

它似乎並沒有被正確讀取JSON飼料。我創建了一個jsfidle以顯示正在發生的事情。

我敢肯定,像往常一樣,我忽略了代碼中的某些東西,但我無法弄清楚我的生活在哪裏/什麼。

編輯: 我應該指出,json提要解析正確。這裏有一個例子:

[{"ScheduleID":"1","StartTime":"3:30 PM","Name":"Registration Opens"},{"ScheduleID":"2","StartTime":"3:30 PM","Name":"State Creed Speaking Order Drawing"},{"ScheduleID":"3","StartTime":"4:30 PM","Name":"State Creed Public Speaking"},{"ScheduleID":"4","StartTime":"7:00 PM","Name":"Opening Session"}] 
+0

那小提琴是顯示:XMLHttpRequest的不能加載的http:/ /oregonffa.com/EventMaster/WebService/ScheduleEvents.php。 Access-Control-Allow-Origin不允許Origin http://fiddle.jshell.net。總之,你無法獲得該資源,因爲它與請求它的頁面不在同一個域中。 –

回答

1

你不能從另一臺主機獲得JSON這個你必須在你的例子控制檯顯示使用JSONP jQuery中

以下錯誤

的XMLHttpRequest無法加載 http://oregonffa.com/EventMaster/WebService/ScheduleEvents.php。來源 http://fiddle.jshell.net不允許通過 訪問控制允許來源。

您可以使用this文章閱讀有關JSONP,但如果你能;噸修改這個服務的話,你可以在你的網站使用代理頁面,

+0

謝謝。隨着更多的谷歌,我發現[這篇文章](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain),這幫助我得到我的JSON飼料糾正,以允許跨域發佈。 – scholzr