JSON ファイルを解析した後、奇妙な問題に直面しています。適切な場所で「トレース」を使用する場合にのみ、データにアクセスして使用できます。トレース行にコメントすると、「未定義」になります...コードの実行順序の問題ですか、それとも文字列引数の受け渡しの問題ですか?
解決策をお探しいただきありがとうございます。この問題は非常にイライラします!!
これが私のコードです:
//index.js
var language={};
var resourceManager = {};
$(document).ready(function(){
loading();
});
function loading() {
$.ajaxSetup({'beforeSend': function(xhr){
if (xhr.overrideMimeType)
    xhr.overrideMimeType("text/plain");
    }
});
$.getJSON("json/lang_french.json", function(data) {
      language = data;
});
setTitle();
}
function setTitle()
{
 var title = resourceManager.getString("welcome");
 var query = document.getElementById('title');
 query.textContent = title;
}
resourceManager.getString = function(str)
{
    //alert(str);//if I uncomment this line, the whole code works...
    return language[str];//when the "alert" is commented, return undefined !!!
};
JSON ファイルは次のとおりです: lang_french.json
{
"welcome" : "Bienvenue",
"goodbye" : "Au revoir"
}
および HTML ファイル index.html
<!DOCTYPE html>
<html>
<head>
 <style>img{ height: 100px; float: left; }</style>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
 <script src="index.js"></script>
</head>
<body>
 <div id="title">
</div>
</body>
</html>