I want to load data from a JSON file in my local using JS/jQuery preferably.
I know there are atleast 10 question on SO with the same answers.
I don't want those. In all of those answers I get a " jquery.min.js:4 XMLHttpRequest cannot load file:///C:/Users/.../js.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
"
Please suggest me a solution to this. If your suggestion is set up a server and host the file there then use it, please help me do it?
Any possible working suggestion is welcome(except the old add json using a script tag, etc). Thanks.
You are making an http request to the local file system, that is not possible.
Check that limited support in HTML5 with the file API but I still doubt its what you want.
使用JSON服务器https://github.com/typicode/json-server来运行本地JSON服务器,而不是直接访问磁盘。
You should use a local webserver such as XAMPP/LAMP/MAMP or IIS/Apache. A related question that should help is here . Another solutions that might work if you really want just filesystem are using iframe and HTML5 FileSystem APIs.
EDIT: When setting XAMPP, after putting your files into htdocs, you might have to set header Access-Control-Allow-Origin *
. This can be achieved with putting this into .htaccess
file in the htdocs directory:
Header set Access-Control-Allow-Origin "*"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.