2016-05-16 10 views
0

の最後に評価された場合にconsole.logは空の配列を提供します。私はtemp1.keywordsの各要素の同義語を取得するためのAPIリクエストを作成することができるよjQueryの - このパターンで配列<code>temp1</code>有する符号

[{"group":"Test","keywords":["Hund","Katze"]}] 

を次の関数で:

consultSynonyms(temp1) 

    function consultSynonyms(x) { 
     // create an empty array where all the responses are gping to be pushed 
     // format has to be: [{x: xvalue, y: yvalue, z:zvalue...}]. This array 
     // will be passed to data table plugin 
     var jsonSynonyms = []; 
     $.each(x, function(i, value) { 
      // get name of group (f.i. Test) 
      var superGroup = value.group; 
      $.each(x[i].keywords, function(i, value) { 
       // get term (f.i. Hund) and iterate all the terms 
       var term = value; 
       $.ajax({ 
        type: "GET", 
        dataType: "jsonp", 
        url: "https://www.openthesaurus.de/synonyme/search?q=" + term + "&format=application/json&supersynsets=true", 
        success: function(data) { 
         $.each(data.synsets, function(key, value) { 
          // get category (f.i. "Biology") from response 
          var category = this.categories[0]; 
          $.each(this.terms, function(key, value) { 
           // synonym is every term delievered by the API Call 
           // level is also delievered (f.i.: "formal", "informal"...) 
           var synonym = this.term; 
           var level = this.level; 
           jsonSynonyms.push({ 
            group: superGroup, 
            keyword: term, 
            category: category, 
            term: synonym, 
            level: level 
           }); 
          }); 
         }); 
        } 
       }); 
      }); 
     }); 
     console.log(jsonSynonyms); 
    } 

しかし、最後console.logはdoesn't期待される出力が、簡単な[]を提供します。私の知る限りcorcernedだとして

[{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Hund"},{"group":"Unnamed group","keyword":"Hund","category":"Biologie","term":"Vierbeiner"}...] 

、終わりにconsole.log:私はfunction(data)内にconsole.logを移動した場合、私が期待した出力を得るので、私は、Ajaxコードが正しいことを確信しています私の関数の最後に評価されるので、私は空の配列を取得している理由を理解していない、関数の途中でそれを評価する正しい出力を提供します。この動作のヒント?

答えて

1

内部でコードの非同期実行を開始するAJAXリクエストを使用します。そして一度非同期に行くと、に戻ることはありません

jsonSynonymsは、まで入力されません。の後にAJAXコールが戻ってきます。それは、おそらく100〜200ミリ秒の時間です。ただし、console.log(jsonSynonyms)コードでは、AJAX呼び出し後にがすぐにに実行されます。したがってjsonSynonymsにデータが入力されていない可能性があります。

は、の後にAJAXコールが戻った後でのみ使用できます。つまり、実際のAJAXコールバック内で使用する必要があります。

function consultSynonyms(x) { 
     // create an empty array where all the responses are gping to be pushed 
     // format has to be: [{x: xvalue, y: yvalue, z:zvalue...}]. This array 
     // will be passed to data table plugin 
     var jsonSynonyms = []; 
     $.each(x, function(i, value) { 
      // get name of group (f.i. Test) 
      var superGroup = value.group; 
      $.each(x[i].keywords, function(i, value) { 
       // get term (f.i. Hund) and iterate all the terms 
       var term = value; 
       $.ajax({ 
        type: "GET", 
        dataType: "jsonp", 
        url: "https://www.openthesaurus.de/synonyme/search?q=" + term + "&format=application/json&supersynsets=true", 
        success: function(data) { 
         $.each(data.synsets, function(key, value) { 
          // get category (f.i. "Biology") from response 
          var category = this.categories[0]; 
          $.each(this.terms, function(key, value) { 
           // synonym is every term delievered by the API Call 
           // level is also delievered (f.i.: "formal", "informal"...) 
           var synonym = this.term; 
           var level = this.level; 
           jsonSynonyms.push({ 
            group: superGroup, 
            keyword: term, 
            category: category, 
            term: synonym, 
            level: level 
           }); 
          }); 
         }); 

         console.log(jsonSynonyms); // look at me now! 
        } 
       }); 
      }); 
     }); 

    } 
関連する問題