Javascript JSON数据和函数

Javascript JSON数据和函数,javascript,ajax,json,Javascript,Ajax,Json,因此,我在理解传递JSON数据时遇到困难 function getFooterContent(){ //ajax call to get json file $.getJSON("scripts/pageData/footer/footerData.json", function(jsonData){ console.log(jsonData); return jsonData; }).fail(function(){ console.log("fail");

因此,我在理解传递JSON数据时遇到困难

function getFooterContent(){
//ajax call to get json file 
$.getJSON("scripts/pageData/footer/footerData.json", function(jsonData){
    console.log(jsonData);
    return jsonData;
}).fail(function(){
    console.log("fail");    
    return -1;
});

//Return the json file 
}

function someFunction(){
    var someContent = new Object();
someContent = getFooterContent();
console.log(someContent);
}

现在我正在调用一个JSON文件。当我使用console.log(jsonData)时,我会得到一个对象,这就是我想要的。这样我就可以了解一些内容了。虽然当jsonData返回到someFunction和I console.log(someContent)时,我没有定义。我不明白,我以为它会是一个对象,就像在getJSON函数中一样。

这是因为
$。getJSON
是异步的。因此,当
getFooterContent()
返回时,此时还没有检索到JSON数据,因此
未定义

您应该做的是让
getFooterContent()
返回一个对象


通过删除变量声明,可以缩短上述示例。我把它们放在里面了,这样代码就更容易理解了,因为getJSON是异步的。因此,当
getFooterContent()
返回时,此时还没有检索到JSON数据,因此
未定义

您应该做的是让
getFooterContent()
返回一个对象


通过删除变量声明,可以缩短上述示例。我把它们放在里面了,这样代码就更容易理解了

getJSON是异步调用的,所以您得不到预期的结果

让我解释一下:

function getFooterContent(){
//ajax call to get json file 
$.getJSON("scripts/pageData/footer/footerData.json", 
  function(jsonData){ // This function is executed when request is ready and server responds with OK
    console.log(jsonData);
    return jsonData;
}).

fail(function(){ // This when servers responds with error
    console.log("fail");    
    return -1;
});

//Return the json file 
// You are returning undefined actually
}

function someFunction(){
    var someContent = new Object();
someContent = getFooterContent();
console.log(someContent);
}
你需要:

function someFunction(){
    var someContent = new Object();
    getFooterContent(function(someContent){
      // someContent is passed by getFooterContent
      console.log(someContent);

    });
}
下面是如何将参数传递给回调

对于您的功能,它将是:

function getFooterContent(done, fail){
  $.getJSON("scripts/pageData/footer/footerData.json", 
  function(jsonData){ // This function is executed when request is ready and server responds with OK
    // console.log(jsonData);
    // return jsonData;
    done.call(null, jsonData); // or done.apply()
}).

fail(function(){ // This when servers responds with error
    // console.log("fail");    
    // return -1;
    fail.call(); // or fail.apply()

});
}

getJSON是异步调用的,因此您无法得到预期的结果

让我解释一下:

function getFooterContent(){
//ajax call to get json file 
$.getJSON("scripts/pageData/footer/footerData.json", 
  function(jsonData){ // This function is executed when request is ready and server responds with OK
    console.log(jsonData);
    return jsonData;
}).

fail(function(){ // This when servers responds with error
    console.log("fail");    
    return -1;
});

//Return the json file 
// You are returning undefined actually
}

function someFunction(){
    var someContent = new Object();
someContent = getFooterContent();
console.log(someContent);
}
你需要:

function someFunction(){
    var someContent = new Object();
    getFooterContent(function(someContent){
      // someContent is passed by getFooterContent
      console.log(someContent);

    });
}
下面是如何将参数传递给回调

对于您的功能,它将是:

function getFooterContent(done, fail){
  $.getJSON("scripts/pageData/footer/footerData.json", 
  function(jsonData){ // This function is executed when request is ready and server responds with OK
    // console.log(jsonData);
    // return jsonData;
    done.call(null, jsonData); // or done.apply()
}).

fail(function(){ // This when servers responds with error
    // console.log("fail");    
    // return -1;
    fail.call(); // or fail.apply()

});
}


所以,如果我想将jsonData保存到一个变量并返回它,我该怎么做呢?好的,希望还有最后一个问题。函数(jsonData)是我正在执行的回调函数,对吗?通过您的示例,getFooterContent执行发送一个函数,其中包含一个参数someContent。这是如何返回到someFunction的?好吧,我想我明白了。我只是不明白最后一点。我现在可以打印一些内容了。虽然当我有someContent=getFooterContent()时;该函数与您的函数getFooterContent(完成,失败)相同。虽然我不知道什么是done和fail,也不知道done.call(null,jsonData);虽然当我删除下面的承诺代码时,它不起作用。
虽然我不明白什么是“完成”和“失败”
这是在其他函数的参数列表中传递函数。所以如果我想将jsonData保存到一个变量并返回它,我会怎么做?好的,希望最后一个问题。函数(jsonData)是我正在执行的回调函数,对吗?通过您的示例,getFooterContent执行发送一个函数,其中包含一个参数someContent。这是如何返回到someFunction的?好吧,我想我明白了。我只是不明白最后一点。我现在可以打印一些内容了。虽然当我有someContent=getFooterContent()时;该函数与您的函数getFooterContent(完成,失败)相同。虽然我不知道什么是done和fail,也不知道done.call(null,jsonData);虽然当我删除下面的承诺代码时,它不起作用。
虽然我不明白什么是“完成”和“失败”
这是在其他函数的参数列表中传递函数。但是我如何访问承诺的数据呢?数据是传递到
完成
函数的第一个参数。查看
console.log(数据)语句。那么我必须将数据保存在该函数中吗?我没有办法在函数外使用数据?我编辑了答案,以显示如何将其分配给函数外的变量,尽管如果我将outsideCopy放在承诺之外,我会未定义?我知道它不是同步的。那么,我是否将outsideCopy传递给函数?那么,我如何访问promise拥有的数据呢?数据是传递到
done
函数的第一个参数。查看
console.log(数据)语句。那么我必须将数据保存在该函数中吗?我没有办法在函数外使用数据?我编辑了答案,以显示如何将其分配给函数外的变量,尽管如果我将outsideCopy放在承诺之外,我会未定义?我知道它不是同步的。那么我是否要将外部副本传递给函数?