2016-05-17 59 views
1

我的應用程序文件夾下有3個文件 - Index.html,Main.js和state.json。以下JavaScript代碼來自Main.js.從state.json獲取數據的正確url格式是什麼?dojo.xhr從具有相對路徑的文件中獲取數據

顯然網址:'/state.json'沒有工作。

dojo.xhrGet({ 
    url: '/state.json', 
    handleAs: json, 
    load: function (result) { 
     require([ 
      'dojo/store/Memory', 
      "dijit/form/FilteringSelect", 
      'dojo/domReady!' 
     ], function (Memory, FilteringSelect) { 
      var stateStore = new Memory({ 
       idProperty: 'code', 
       data: result.states.sort(function(a,b) { 
        var x = a.name.toLowerCase(); 
        var y = b.name.toLowerCase(); 
        return x < y ? -1 : x > y ? 1 : 0; 
       }) 
      }); 

      var cboState = new FilteringSelect({ 
       id: 'usastate', 
       name: 'usastate', 
       style:{width: '100%', height: '35px', fontSize: '30px'}, 
       placeholder: 'Select a State', 
       store: stateStore, 
       searchAttr: 'name', 
       autocomplete: true, 
       onChange: function(value) { 
        dom.byId('statecode').innerHTML = value; 
       } 
      }); 

      cboState.placeAt(dom.byId('state')).startup(); 

     }); 
    } 
}); 
+0

直接使用'網址: 'state.json',' –

回答

0

使用此快速參考找到正確的路徑。您應該使用

/  = Root directory 
.  = This location 
..  = Up a directory 
./  = Current directory 
../ = Parent of current directory 
../../ = Two directories backwards 

要回復你的問題,嘗試使用以下,如果的Index.html,Main.js和state.json都在同一個文件夾:

url: './state.json', 
相關問題