0

I'm trying to load a JSON file that I'll use as constants file, but I'm always getting the error undefined, could any one help?

Here is the code:

function loadJsonFile(filePath){
 $.getJSON(filePath, function(Mydata) {
    alert(Mydata.length);
    return Mydata;
    })
error(function() { alert("error"); 
});

The structure of constants file is:

{
    "var1":"v1",
    "var2":"v2"
}

Thank you in advance for your help.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • 1
    Where are you getting error undefined, what line? also you are missing a dot . between `)` and `error` is that a typo from typing this question or is it really missing the dot – Patrick Evans Apr 24 '15 at 20:35
  • make sure the file path you are giving is correct, let us know what error you are getting in network tab of developers tool – vinayakj Apr 24 '15 at 20:35
  • @PatrickEvans it was an error while I was typing the question, i get the error when I call my function : var data= loadJsonFile('./constants.json'); alert(data["var1"]); – yatika mika Apr 24 '15 at 20:40
  • @vinayakj yes the path is correct, i get as an error : " Uncaught TypeError: Cannot read property 'var1' of undefined – yatika mika Apr 24 '15 at 20:41
  • you need to alert in success callback, you are alerting it before even you get the response. not like this var data= loadJsonFile('./constants.json'); alert(data["var1"]); – vinayakj Apr 24 '15 at 20:45
  • @vinayakj same error :/ – yatika mika Apr 24 '15 at 20:48
  • 2
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Patrick Evans Apr 24 '15 at 20:54
  • @yatikamika try my answer given below – vinayakj Apr 24 '15 at 20:57
  • @PatrickEvans is right, yatika you either need to make AJAX synchronuos to return the response or else you need to have callback function to do your work, you cant return response in asynchrnous as it its name indicates. – vinayakj Apr 24 '15 at 21:00
  • @vinayakj "Make AJAX synchronous" is never the answer – joews Apr 24 '15 at 21:49
  • I must pass by doStuff function? I only wanna get the list of varsin my main function – yatika mika Apr 24 '15 at 22:00
  • Then pass main as callback function instead of doStuff, doStuff is just a placeholder function I wrote. you can use like loadJsonFile('url', yourMainFunction); – vinayakj Apr 25 '15 at 09:22

3 Answers3

0

For a call to loadJsonFile to return a value, the return statement has to be placed outwith the $.getJSON() success callback function. There's also some syntax errors in the failure handler, and .fail() should be used instead as .error() is deprecated.

function loadJsonFile(filePath) {
    var data = null;
    $.getJSON(filePath, function(Mydata) {
        alert(Mydata.length);
        data = Mydata;
    })
    .fail(function() {
        alert("error");
    });
    return data;
}
drewb
  • 91
  • 9
0
function loadJsonFile(filePath, callback){
       $.getJSON(filePath)
        .success(function(Mydata) {
              callback(data);
       })
       .fail(function() { alert("error")}); ;
});

function doStuff(data){
  alert(data.var1)
}

loadJsonFile('url', doStuff);
vinayakj
  • 5,591
  • 3
  • 28
  • 48
-2

Two things: 1. Are you missing a JSON.parse() ?

Mydata = JSON.parse(Mydata); // we parse it here
alert(Mydata.length);
return Mydata;
  1. Remember a valid json object must be between comillas ("|'). If you do

    JSON.parse({ "var1":"v1", "var2":"v2" });

you would get error. Correct way is:

JSON.parse('{ "var1":"v1", "var2":"v2" }');
Oxcarga
  • 260
  • 3
  • 9