How to read a json file stored in c drive using jquery

136
May 10, 2018, at 3:34 PM

I want to read a json file stored in C:/Program Files/Sample/demo.json using JQuery. I am using the following code but it is not picking the file. I want to read it only from that path.

$.getJSON("C:/Program Files/Sample/demo.json",function (data) {
    $.each(data, function(i, data) {

Please help me on this.

Answer 1

If you're doing this from a page loaded via http: or https:, you can't do this. It would be a major security issue.

If you're doing this from a file loaded via a file: URL, some browsers will allow it, and others won't. For instance, Chrome doesn't, Firefox does.

In general, better to use a web server (even if a locally-installed one) for web tasks, and serve both the page and the JSON file through it.

Answer 2

C:/Program Files/Sample/demo.json is an absolute path, not a relative path. When building web applications, you need to use a relative path, from your Javasctipt source (.js files) to your target files (.json file).

For example, if you keep both files in the same directory, just use:

$.getJSON("demo.json", ....)

If you put the .json file in a json directory under your root directory, you should use:

$.getJSON("/json/demo.json", ....)

Read more about relative paths here.

READ ALSO
How to write an img element with several double quotation marks when using “append”

How to write an img element with several double quotation marks when using “append”

I am trying to use "append" to add an img element on my htmlBut the img tag includes several double quotation marks

89
How to set interval after clearing it

How to set interval after clearing it

I can set interval and clear it on clickI want to setInterval again but don't know how to do

72
I need codes for next and prev button in pagination in javascript

I need codes for next and prev button in pagination in javascript

I don't know what code to be written in button next and prevHere I add these codes

60
Add second onmouseover action

Add second onmouseover action

I have the following script:

71