1

I have a local .json formated file that I like to get it via $.getJSON as follows:

$.getJSON('D:/Data/cpu.json', function(data) {

this does not seem to be working. Any ideas what might be the case?

user1471980
  • 10,127
  • 48
  • 136
  • 235

2 Answers2

1

What you need to do use a "file URI scheme".

The file URL scheme is a URL scheme specified in RFC 1630 and RFC 1738, typically used to retrieve files from within one's own computer.

It looks something like this:

file://path/to/your/file

So, for your example:

$.getJSON('file://D:/Data/cpu.json', function(data) { ... } );

There are some considerations to be made when using a windows machine. You can take a look at the wiki article I linked to. It is related to the colon within the file path.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • note: You can only do this if the requester is opened from a `file://` URI too... Otherwise the file protocol cannot return the allow origin header and the response will be blocked. (I think the request too, because accessing local files from websites is forbidden.) – inf3rno May 27 '14 at 18:16
0

One possible reason will be CORS.

The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin.

Means this is not possible with the system file, you have to use it in a web server.

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164