jQuery:在AJAX成功中调用递归函数

时间:2015-05-29 23:00:19

标签: javascript jquery ajax recursion

我遇到了问题:

function getArbre (recherche, div){
    $.ajax({
        //async : false,
        type    : "GET",
        dataType: "json",
        timeout : 2000,
        url     : "test.php?recherche=" + recherche,
        success : function(donnees) {
            if (donnees.Error) {
                alert("Erreur");
            } else {
                console.log(donnees);                                       
                myjson = donnees;
            }           
        },
        complete : function(donnees) {
            console.log("completed");
            //JSONToTree(myjson);
        },
        error: function(donnees, status, err) { 
            console.log(status + " : " + err); 
        }
    }).then(function(data){
        JSONToTree(myjson);
    });

还有我的功能:

function JSONToTree(json){  
    console.log(json.id);       
    for (i = 0; i < json.Children.length; i++) {        
        JSONToTree(json.Children[i].id);            
    }
}

我有问题,我无法阅读JSON的内容,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:-1)

我认为您正在获取JSON字符串并需要解析它。你可以这样做

function JSONToTree(json){
   var jsonObj = JSON.parse(json);  
   console.log(jsonObj.id);       
   for (i = 0; i < jsonObj.Children.length; i++) {        
      JSONToTree(jsonObj.Children[i].id);            
   }
}
相关问题