Download json from a text file

I have the following json hard code,

var dataLocality = [
    { "label": "Arwen" },
    { "label": "Bilbo Baggins" },
    { "label": "Boromir" },
    { "label": "Frodo Baggins" },
    { "label": "Peregrin Pippin Took" },
    { "label": "Samwise Gamgee" }
];

What I use to fill in an autocomplete text box with the following script,

$(function () {
    $("#locality").autocomplete(
    {
        source: dataLocality
    })
});

Now I have a text file that is dynamically updated through my application called dataLocality.text, which I can download and view in a warning window with this code,

function codeAddress() {
    jQuery.get('http://localhost/project/jSonDocs/dataWhat.txt', function (data) {
        var dataLocality = data;
        alert(dataLocality);
    });
}
window.onload = codeAddress;

But I can’t figure out how to get data from var dataLocalitytosource: dataLocality

The data in my text document is as follows:

[
    { "label": "Arwen" },
    { "label": "Bilbo Baggins" },
    { "label": "Boromir" },
    { "label": "Frodo Baggins" },
    { "label": "Peregrin Pippin Took" },
    { "label": "Samwise Gamgee" }
];
+4
source share
1 answer

Assuming you are using the jQueryUI autocomplete method, you can specify the JSON URL for the method sourceand it will automatically retrieve it for you. Try the following:

$("#locality").autocomplete({
    source: 'http://localhost/project/jSonDocs/dataWhat.txt'
});

- JSON ( , ), :

function codeAddress() {
    jQuery.get('http://localhost/project/jSonDocs/dataWhat.txt', function (data) {
        $('#locality').autocomplete('option', 'source', data);
    });
}
+7

All Articles