How can I add a JSON file in jsfiddle? I have a JSON file but I am not able to attach it in jsfiddle. I can make a JSON object and use it, but is there any way to add an external JSON file to a fiddle?
相关问题
- Is there a limit to how many levels you can nest i
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to toggle on Order in ReactJS
- StackExchange API - Deserialize Date in JSON Respo
Myjson.com provides api, which runs in Jsfiddle.net.
Custom my myjson:
Myjson GET Example:
You can harness the power of Cross-Origin Resource Sharing (CORS) to achieve your task.
Basically how CORS works is that if the
Access-Control-Allow-Orign
header is set in the HTTP response, then the content loaded by AJAX can be used in our script regardless of the fact it is on the same domain or some other.Now for your purpose, you can upload your local JSON file to Dropbox's
Public
folder and get a Public URL, that you can load by a simple AJAX call.The AJAX call will succeed in this case because Dropbox sets the following value in the response
Access-Control-Allow-Orign:*
which means any domain can use the loaded content.Jquery code will be something like this(you can even use pure JavaScript if you prefer ).
Example JSFiddle
Based on your comment, you want to use a pure JSON file as an external resource in a jsFiddle. You can't do this, because pure JSON is not JavaScript. Say you try to include
http://example.com/foo.json
as an external resource, and that file contains the following:This will result in
Uncaught SyntaxError: Unexpected token :
, because the JSON object is not valid JavaScript by itself.But if you assign the JSON object to a variable, like so:
then no problem.
Solution: use a modified version of your file to initialize a variable for use in the jsFiddle.