Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have populated the dropdown box with JSON structure inside the JS file , but I need to the JOSN data from external file.

What I have tried:

var jsonData = {
                                   "Table": [{
                                       "stateid": "2",
                                       "statename": "AP"
                                   }, {
                                       "stateid": "3",
                                       "statename": "TL"
                                   }, {
                                       "stateid": "4",
                                       "statename": "Kewrala"
                                   }, {
                                       "stateid": "5",
                                       "statename": "TN"
                                   }, {
                                       "stateid": "6",
                                       "statename": "WR"
                                   }]
                               };

$(document).ready(function () {
    var listItems = '<option selected="selected" value="0">- Select -</option>';

    for (var i = 0; i < jsonData.Table.length; i++) {
        listItems += "<option value='" + jsonData.Table[i].stateid + "'>" + jsonData.Table[i].statename + "</option>";
    }

    $("#DLState").html(listItems);
});
Posted
Updated 21-Aug-18 2:41am
Comments
F-ES Sitecore 21-Aug-18 6:20am    
The solution depends on where this file is which you have failed to say.
SureshMunna 21-Aug-18 6:32am    
I didn't get
SureshMunna 21-Aug-18 7:03am    
tell me what should I do for that??
F-ES Sitecore 21-Aug-18 7:06am    
Where is the file you are trying to read?
SureshMunna 21-Aug-18 7:43am    
I have added the JSON file to my webapplication solution in Visual studio.In the above mentioned snippet I have written Json file date in the script, instead of that I wanna read that content form data.json file which I have added to my project solution in visual studio

1 solution

Use jQuery's getJSON

jQuery getJSON() Method[^]

You would do that in the document ready event, then the code you have in document ready you would move to the "success" event of getJSON where "result" in that link matches "jsonData" in your existing code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900