2014-01-30 70 views
0

我正在從網上測試一個應用程序,試圖瞭解jQuery數據在jQuery移動中的使用。在原始應用程序中,json數據在javascript文件中明確列出。我提取數據並將其保存在本地驅動器的目錄中(與html文件和javascript文件位置相同)。更改後,由於文件訪問問題,頁面停止工作。我搜索谷歌整整一天,但沒有找到解決方案。使用javascript訪問本地json數據

HTML文件:

<!DOCTYPE html> 
<html> 
<head> 
<meta name=viewport content="user-scalable=no,width=device-width" /> 
<title>Test data listview</title> 
<link rel="stylesheet" href="../download18Dec13/jquery.mobile-1.3.2.min.css" /> 
<script src="../download18Dec13/jquery-1.9.1.min.js"></script> 
<script src="../download18Dec13/jquery.mobile-1.3.2.min.js"></script> 
<script src="test.js"></script> 
<link rel="stylesheet" href="test.css"/> 
</head> 

<!--first page --> 
<div data-role="page" id="info-page"> 
    <div data-role="header" data-theme="b"> 
      <h1> Information</h1> 

     </div> 
     <div data-role="content"> 
     <ul data-role="listview" id="prof-list" data-divider-theme="a" data-  inset="true"> 
     <li data-role="list-divider" data-theme="b" role="heading">Names</li> 
     </ul> 
     </div> 
    </div> 
    <!--second page --> 
    <div data-role="page" id="details-page"> 
    <div data-role="header" data-theme="b"><a href="#" data-rel="back" data- role="button">Go back</a> 

     <h1>Employee Details</h1> 

    </div> 
    <div data-role="content"></div> 
</div> 
</html> 

JavaScript文件:

//assuming this comes from an ajax call 
    //alert(window.location.href); 
    response.setHeader("Access-Control-Allow-Origin", "data.js"); 
    var info=$.getJSON('data.js',function(data){ 
     //alert("JSON Data: " + data[ 3 ].name); 
     //var info=data; 
    }); 

    //var info=data; 
     //.done(function() { 
       //alert("second success"); 
     //}) 
     //.fail(function() { 
      //alert("error"); 
     //}) 
     //.always(function() { 
       //alert("complete"); 
     //}); 
    //alert(info.always()); 
    //alert($.document.url()); 
    //pageinit event for first page 
    //triggers only once 
    //write all your on-load functions and event handlers pertaining to page1 
    //var info = new Spry.Data.JSONDataSet("data.js"); 
    //alert(infor[3].name); 
     //alert("ffff"); 
    $(document).on("pageinit", "#info-page", function() { 


     //set up string for adding <li/> 
     var li = ""; 
     //container for $li to be added 
     $.each(info, function (i, name) { 
      //add the <li> to "li" variable 
      //note the use of += in the variable 
      //meaning I'm adding to the existing data. not replacing it. 
      //store index value in array as id of the <a> tag 
      li += '<li><a href="#" id="' + i + '" class="info-go">' + name.name + '</a> </li>'; 
     }); 
     //append list to ul 
     $("#prof-list").append(li).promise().done(function() { 
      //wait for append to finish - thats why you use a promise() 
      //done() will run after append is done 
      //add the click event for the redirection to happen to #details-page 
      $(this).on("click", ".info-go", function (e) { 
       e.preventDefault(); 
       //store the information in the next page's data 
       $("#details-page").data("info", info[this.id]); 
       //change the page # to second page. 
       //Now the URL in the address bar will read index.html#details-page 
       //where #details-page is the "id" of the second page 
       //we're gonna redirect to that now using changePage() method 
       $.mobile.changePage("#details-page"); 
      }); 

      //refresh list to enhance its styling. 
      $(this).listview("refresh"); 
     }); 
    }); 

    //use pagebeforeshow 
    //DONT USE PAGEINIT! 
    //the reason is you want this to happen every single time 
    //pageinit will happen only once 
    $(document).on("pagebeforeshow", "#details-page", function() { 
     //get from data - you put this here when the "a" wa clicked in the previous page 
     var info = $(this).data("info"); 
     //string to put HTML in 
     var info_view = ""; 
     //use for..in to iterate through object 
     for (var key in info) { 
      //Im using grid layout here. 
      //use any kind of layout you want. 
      //key is the key of the property in the object 
      //if obj = {name: 'k'} 
      //key = name, value = k 
      info_view += '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar field" style="font-weight : bold; text-align: left;">' + key + '</div></div><div class="ui- block-b"> 
    <div class="ui-bar value" style="width : 75%">' + info[key] + '</div></div></div>'; 
     } 
     //add this to html 
     $(this).find("[data-role=content]").html(info_view); 
    }); 

Json data (To save space, only three items listed): 
     { 
     { 
      "id": 0, 
       "age": 31, 
       "name": "Avis Greene", 
       "gender": "female", 
       "company": "Handshake", 
       "email": "[email protected]", 
       "phone": "+1 (845) 575-2978", 
       "address": "518 Forrest Street, Washington, New York, 3579" 
     }, { 
      "id": 1, 
       "age": 31, 
       "name": "Dunn Haynes", 
       "gender": "male", 
       "company": "Signity", 
       "email": "[email protected]", 
       "phone": "+1 (829) 454-3806", 
       "address": "293 Dean Street, Dante, Oregon, 5864" 
    }, { 
     "id": 2, 
      "age": 20, 
      "name": "Contreras Keith", 
      "gender": "male", 
      "company": "Overfork", 
      "email": "[email protected]", 
      "phone": "+1 (941) 412-2874", 
      "address": "166 Broome Street, Norris, Kentucky, 2163" 
    }, { 
     "id": 3, 
      "age": 27, 
      "name": "Hays Schneider", 
      "gender": "male", 
      "company": "Orbean", 
      "email": "[email protected]", 
      "phone": "+1 (896) 599-2026", 
      "address": "889 Engert Avenue, Staples, Illinois, 9927" 
    } 
    } 

回答

0

這不是一個JavaScript的問題,或者你有一個JSON問題。
這是一個瀏覽器安全問題。長期以來,您想要在您的計算機/設備上運行網頁(即:直接從您的驅動器到您的瀏覽器),然後還可以從該文件進行AJAX調用,然後使用硬盤驅動器獲取JSON。

瀏覽器製造商決定,這將是一個非常糟糕的主意,讓你繼續這樣做。
推理非常簡單......如果有人可以在你的系統上運行一個.html文件,那麼它要求的任何JS都可以訪問你的計算機上的任何文件,並將該數據發送給任何其他計算機......那會很糟糕。

無論如何,這個特殊故事的寓意是你需要一個服務器。
您可以在本地機器上運行服務器,從瀏覽器連接到該服務器,然後服務器可以加載頁面/ JSON並將其發送回瀏覽器,每個人都很開心。然後,確保服務器不會放棄所有事情,這是服務器的工作。

如果您使用的是PC,那麼現在就開始使用PHP服務器並非常容易。
Python也有一個簡單的服務器。

+0

添加到什麼Norguard剛纔說的,你可以隨時使用的Dropbox的公用文件夾(https://www.dropbox.com/help/16/en)在線存儲靜態。比讓PHP服務器啓動並運行更容易。 – GabrielG

0

你的json格式不正確。現在它形成良好。

dada.json文件

{ 
"data":[{ 
    "id": 0, 
    "age": 31, 
    "name": "Avis Greene", 
    "gender": "female", 
    "company": "Handshake", 
    "email": "[email protected]", 
    "phone": "+1 (845) 575-2978", 
    "address": "518 Forrest Street, Washington, New York, 3579" 
}, { 
    "id": 1, 
    "age": 31, 
    "name": "Dunn Haynes", 
    "gender": "male", 
    "company": "Signity", 
    "email": "[email protected]", 
    "phone": "+1 (829) 454-3806", 
    "address": "293 Dean Street, Dante, Oregon, 5864" 
}, { 
    "id": 2, 
    "age": 20, 
    "name": "Contreras Keith", 
    "gender": "male", 
    "company": "Overfork", 
    "email": "[email protected]", 
    "phone": "+1 (941) 412-2874", 
    "address": "166 Broome Street, Norris, Kentucky, 2163" 
}, { 
    "id": 3, 
    "age": 27, 
    "name": "Hays Schneider", 
    "gender": "male", 
    "company": "Orbean", 
    "email": "[email protected]", 
    "phone": "+1 (896) 599-2026", 
    "address": "889 Engert Avenue, Staples, Illinois, 9927" 
}] 
} 


<script> 
$(document).on("pageinit", "#info-page", function() { 
    var li = '<li data-role="list-divider" data-theme="b" role="heading">Names</li>'; 
    $.getJSON("data.json", function (json, status){ 
     var data = json.data; 
     $.each(data, function(key, value){ 
      $.each(value, function(key, value){     
       if(key == 'name'){ 
        li += '<li><a href="#" id="' + key + '" class="info-go">' + value + '</a> </li>'; 
       } 
      }) 
     }) 
     $("#prof-list").append(li).trigger('create'); 
      $("#prof-list").listview('refresh');     
    }) 
    .success(function(result) {}) 
    .fail(function(jqXHR, textStatus, errorThrown) {}) 
    .complete(function() {}); 
}) 
</script>