jsonurldojodojo.xhrget

dojo.xhrGet fetch data from a file with a relative path


I have 3 files under my application folder - Index.html, Main.js, and state.json. The following javascript codes are from the Main.js. What is correct url format to fetch data from the state.json?

Apparently url: '/state.json' didn't work.

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();

        });
    }
});

Solution

  • Use this quick reference to find the right path. You should use

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

    To reply your question, try to use the following, if Index.html, Main.js, and state.json are on the same folder:

    url: './state.json',