Karthikeyan_kk Karthikeyan_kk - 4 months ago 17
Javascript Question

How to read data from a text file in Jquery / JS to display it in html page?

{
"symbol": "BPCL",
"series": "EQ",
"openPrice": "1,020.00",
"highPrice": "1,041.95",
"lowPrice": "1,016.55",
"ltp": "1,040.55",
"previousPrice": "1,013.45",
"netPrice": "2.67",
"tradedQuantity": "3,76,694",
"turnoverInLakhs": "3,892.87",
"lastCorpAnnouncementDate": "13-Jul-2016",
"lastCorpAnnouncement": "Bonus 1:1"
}


This is sample data given in gainers.txt file i want to parse these information and store it on separate variable eg var symbol should store all the symbols in txt file so that i can process with that variables later in my code.

Answer

You need to add a dataType:

$(document).ready(function() {
    $.ajax({
        url : "mytext.txt",
        dataType: "text",
        success : function (data) {
            $(".text").html(data);
            var jsonData = JSON.parse(data);
        }
    });
}); 

You can use jsonData as data json object format, or use data instead for text format.

Comments