2016-03-29 12 views
0

htmlページからローカルJsonファイルを読みたいと思います。しかし、クロムとIEで動作するHTMLページで、ローカルのJsonファイルを読むことができません。 Webサーバーを使用せずに実行する方法はありますか?サーバーなしでhtmlページ内のローカルJsonファイルを読むにはどうすればいいですか

+0

参照してください。この回答 http://stackoverflow.com/questions/19706046/how-to-read-an-external-local-json-file-in-javascript –

答えて

0

index.htmlを同じフォルダ内に& sample.json、あなたがこれを行うことができ 、あなたが持っていると言う

もちろんの
$http.get('sample.json').then(function(response) { 
    console.log(response); 
}); 

あなたは一人で立って、コントローラからこれを実行する必要がありますかなど

0

ディレクティブに私はウェブ上でこの解決策を見つけ、私はそれを試していないが、コメントによると、それは動作するはず

function loadJSON(callback) { 

    var xobj = new XMLHttpRequest(); 
     xobj.overrideMimeType("application/json"); 
    xobj.open('GET', 'my_data.json', true); // Replace 'my_data' with the path to your file 
    xobj.onreadystatechange = function() { 
      if (xobj.readyState == 4 && xobj.status == "200") { 
      // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode 
      callback(xobj.responseText); 
      } 
    }; 
    xobj.send(null); 
} 

The function above will create a new instance of a XMLHttpRequest and load asynchronously the contents of my_data.json. I have gone with asynchronous but you can change the argument to false if you want a synchronous load. Thankfully all modern browsers support the native JSON.parse method. Remember our anonymous callback? here's how you use it. 

function init() { 
loadJSON(function(response) { 
    // Parse JSON string into object 
    var actual_JSON = JSON.parse(response); 
}); 
}`` 

http://codepen.io/KryptoniteDove/post/load-json-file-locally-using-pure-javascript

-1

JSONファイルに存在する値を取得するには、以下のコードを使用して、コントローラでの翻訳フォルダ.Thenにsample.jsonという名前のJSONファイルを作成します

$http.get('translation/sample.json').success(function(response){ 
     console.log(response);   
}); 

または

$.getJSON('translation/sample.json', function(data){ 
    console.log(data); 
}); 
+0

あなたの質問を編集することができます、説明はコミュニティのためではなく、私;) – GGO

関連する問題