1

I have a button click that tries to load a json file. It never reaches the success function. I presume I can load a json file locally and not have to have it on a server? Why would this not work.

$(function(){
    $('.demo').click(function(){

            $.ajax({
            url: 'data/demo.json',
            dataType: 'json',
            success: function(data){
                console.log("data = ", data)
                        }
        });

    });
});

here is my folder structure tree.

  • index.html
  • data (folder)
    • demo.json js
  • page.js

JSON

{"markers": [
        {
            "point":new GLatLng(40.266044,-74.718479),
            "homeTeam":"Lawrence Library",
            "awayTeam":"LUGip",
            "markerImage":"images/red.png",
            "information": "Linux users group meets second Wednesday of each month.",
            "fixture":"Wednesday 7pm",
            "capacity":"",
            "previousScore":""
        },
        {
            "point":new GLatLng(40.211600,-74.695702),
            "homeTeam":"Hamilton Library",
            "awayTeam":"LUGip HW SIG",
            "markerImage":"images/white.png",
            "information": "Linux users can meet the first Tuesday of the month to work out harward and configuration issues.",
            "fixture":"Tuesday 7pm",
            "capacity":"",
            "tv":""
        },
        {
            "point":new GLatLng(40.294535,-74.682012),
            "homeTeam":"Applebees",
            "awayTeam":"After LUPip Mtg Spot",
            "markerImage":"images/newcastle.png",
            "information": "Some of us go there after the main LUGip meeting, drink brews, and talk.",
            "fixture":"Wednesday whenever",
            "capacity":"2 to 4 pints",
            "tv":""
        },
] }
0

2 Answers 2

1

Well I have a similar script, and it accesses a local file and works fine. The problem I see here is your JSON is invalid. Maybe thats the problem?

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

3 Comments

Ok thats good to know as everyone else here says you can't. Can you give me a valid json files so I can test. I nabbed this one from online as a tester so I presumed it was valid.
Run it through jsonlint.com. The errors are the"point": newGLatLng(40.294535, -74.68201) lines. They need to be surrounded by double quotes. It would work if there was a function changing those values, but if you are using it as a static file, it will never work.
Thank you Dean that worked. I'm glad you cleared up that you could in fact load json locally.
0

You cannot use AJAX to access local files even if the page is served locally from a file

2 Comments

Ok can you give me an example of how I would load this file?
@JuanMendes Are there other access methods to local files than AJAX?

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.