0

I want to load a JSON file locally into my HTML page, without using a web server. This isn't possible with AJAX so I came up with this solution:

HTML:

<script src="../js/hack.js"></script>
<script src="../js/data.json"></script>

Inside hack.js :

var myJsonData = 

And the json file looks like this:

[{"something":"anything"},{},{}]

I expected that the ultimate result would be this:

var myJsonData = [{"something":"anything"},{},{}]

But sadly I get the error: Uncaught SyntaxError: Unexpected end of input after loading 'hack.js'.

Is there any way to solve this without adjusting the json file, and without adjusting the local browser settings / using a localhost server?

5
  • Save that file as data.js and try to get the value! Commented Sep 30, 2014 at 11:13
  • 1
    You cannot expect that the files are just concatinated. Each file is parsed as a seperated script, you can use each others variables, but there is no overflow from the code Commented Sep 30, 2014 at 11:13
  • you cannot do like this..you need to have an ajax call to the json to get the values from it and then fill myJsonData variable with the json data Commented Sep 30, 2014 at 11:13
  • stackoverflow.com/a/18278346/609907 Commented Sep 30, 2014 at 11:14
  • He is using the code localy, he isn't able to do ajax requests. Commented Sep 30, 2014 at 11:15

1 Answer 1

1

You will have to change your json file so that you declare the variable there.

data.json

var MyjsonData = [{"something":"anything"},{},{}]

Now in your hack.js file, you can call the variable MyjsonData.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's probably the only solution. I was hoping for a way to avoid altering the JSON file itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.