2

目前我正在使用以下方式用 json 获得的数据填充我的全局 js 变量:

var tranlationJson =
  $.ajax({
    type: "GET",
    url: "translation.xml",
    contentType: "text/xml",
    dataType: "xml",
    success: function (dataSource) {            
            tranlationJson=ToJasonParser(dataSource);
    }
});

有没有更聪明的方法?我需要填充这些变量,因为在稍后加载的脚本中我使用它们的内容。

4

1 回答 1

1

您可以使 tranlationJson 成为对象,而不是像这样的变量:

var tranlationJson = {
    init: function(){
              $.ajax({
                  type: "GET",
                  url: "translation.xml",
                  contentType: "text/xml",
                  dataType: "xml",
                  success: function (dataSource) {            
                      this.data = ToJasonParser(dataSource);
                  }
              });
}

然后像这样调用init函数:

tranlationJson.init();

那么你可以Json response data像这样访问:

tranlationJson.data.something;

演示

于 2013-04-25T14:51:36.870 回答