0

I am trying to read a json file to a javascript variable. I have read the same file previously. So the json file is in correct format.

var net_json = {};
$.ajax({
    type: "GET",
    url: "/karate.json",
    dataType: "json",
    error: function() {
        alert("Error loading the file");
    },
    success: function(data) {
        net_json = data;
        alert("Success");
    }
});

But it always returns alert Error loading the file. Both json and js files are in the same location.

10
  • What is the location of karate.json w.r.t. the document/page where the above JS code is executed? Commented Jul 31, 2015 at 4:21
  • What does it say in the network panel? Do you realize that the path is not based off the location of the Js file but the page it is on? Commented Jul 31, 2015 at 4:22
  • 1
    error:function(jqXHR, textStatus, errorThrown){ alert(jqXHR.responseText);} catch the error with this and see what you get as responseText! Commented Jul 31, 2015 at 4:22
  • 1
    Oh, this isn't my question, man. He's just gotta hit f12, and all his questions will be answered. I had just made a useless comment above, haha. I'll just leave now... Commented Jul 31, 2015 at 4:39
  • 1
    It is not f12 in all operating systems. ;) Commented Jul 31, 2015 at 4:40

1 Answer 1

2

My guess is your structure is like this

  • index.html
  • js/myJS.js
  • js/karate.json

which means the url should be

url:"js/karate.json",

The relative path is based on the page, not where the JavaScript file resides.

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

Comments

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.