-1

My requirement is to read the json file which contains some data and store in some other .js file.

I got task to read local file from local disk in Javascript , i have used file path like - D:\json\analytics.json.

(document).ready(){
($).getData("D:\json\analytics.json");

}

when i see in firebug it takes other url.

How I can do it, is it possible to read file from javascript.

I don't know javascript , i have seen some answer but i am not able to understand .

Need Solution , how I can achieve it. is there any other way to read file on jsp without using scriptlet . From server side , can send it on the jsp page.

sky
  • 93
  • 2
  • 3
  • 15

1 Answers1

1

I think Jaronmanda's answer won't work cause it will hit cross origin issue, see "Cross origin requests are only supported for HTTP." error when loading a local file.

As the page suggested, in general you need to serve that json file from a web service (same domain, or allow your domain to access), but it depends on what you really need to do. If you can control where that json file is stored, the easier way is to put that in a subdirectory of your html file, and do:

$(document).ready(function () {       
    $.get('<directory>/analytics.json', function (data) {
        // Do your stuff
    });
});
Community
  • 1
  • 1
Natural Lam
  • 732
  • 4
  • 13
  • `I think Jaronmanda's answer won't work cause it will hit cross origin issue` - I never posted an answer, I merely informed the OP of the correct syntax for file protocol. If the page is loaded using file:///, the there is a small chance he could read that file ... but that was not part of the question anyway – Jaromanda X Oct 18 '15 at 07:21
  • @Natural Lam Thanks for your answer , I have done same way. Thanks for your answer, it was my first experience with javascript , after solving this , i got experience with javascript , :) – sky Nov 21 '15 at 14:09