2017-11-11 85 views
0

我试图通过AJAX将GET请求的一部分结果存储到字符串变量中时出现问题。将AJAX GET响应的一部分(以JSON形式)存储到字符串变量中的问题

基本上,我想使它包含GET请求操作的某个函数将返回该操作的结果。

var count = 0; 

$.getJSON("https://api.icndb.com/jokes/count", function(data){ 
    count = data.value+1; 
    for (i = 1; i < count; i++){ 
     if (i != 1) { 
      setTimeout(jokeGet, i*7500, i); 
     } 
     else { 
      jokeGet(i); 
     } 
    } 
}); 

function jokeGet(n) { 
    var str = ""; 
    $.getJSON("https://api.icndb.com/jokes/" + n, function(data){ 
     if (data.type != "NoSuchQuoteException") { 
      $(".joke").html(data.value.joke); 
      str = data.value.joke; 
     } 
     else { 
      count++; 
     } 
    }); 

    return str; 
} 

API我正在发出请求以将信息存储在JSON树中。下面是这种树的两个例子:

{ "type": "success", "value": { "id": 1, "joke": "Chuck Norris uses ribbed condoms inside out, so he gets the pleasure.", "categories": ["explicit"] } } 

{ "type": "NoSuchQuoteException", "value": "No quote with id=8." } 

但是,每当我运行单元测试(通过QUnit),事实证明,在任何及所有情况下,jokeGet()函数返回一个空字符串。这是我觉得很奇怪的事情,因为我认为str = data.value.joke系列会让它变成这样,这个笑话被存储在变量str中。

显然,因为str总是以空字符串的形式返回,所以情况并非如此。有什么建议,为什么这是?

更新

考虑到的是什么,我现在在做的目的不是为了让程序工作,但做单元测试,以证明程序工作,我已决定将“单元测试”文件:

QUnit.test("cn_jokes", function(assert) { 
    function joke(n, expected) { 
     assert.equal(jokeGet(n), expected); 
    } 
    joke(1, "Chuck Norris uses ribbed condoms inside out, so he gets the pleasure."); 
    joke(8, undefined); 
    joke(163, "Ninjas want to grow up to be just like Chuck Norris. But usually they grow up just to be killed by Chuck Norris."); 
    joke(221, "Chuck Norris is the only person to ever win a staring contest against Ray Charles and Stevie Wonder."); 
    joke(352, "Chuck Norris doesn't see dead people. He makes people dead."); 
    joke(502, "Chuck Norris insists on strongly-typed programming languages."); 
    joke(526, "No one has ever pair-programmed with Chuck Norris and lived to tell about it."); 
    joke(598, "Once Chuck Norris and Superman had a competition. The loser had to wear his underwear over his pants."); 
}); 

正如你可以看到,我想获得的jokeGet()功能,具体,返回笑话值。请让我知道这是否可能。

回答

2

$.getJSON是异步的前返回值;您的代码将在请求发出时继续运行。正因为如此,str很久之前通过您传递给getJSON运行的回调返回。你或许应该有jokeGet需要一个回调函数,并调用它的请求完成(通过data.value.joke作为参数)时:

function jokeGet(n, callback) { 
    $.getJSON("https://api.icndb.com/jokes/" + n, function(data){ 
     if (data.type != "NoSuchQuoteException") { 
      $(".joke").html(data.value.joke); 
      if (callback !== undefined && callback !== null) 
       callback(data.value.joke); 
     } 
     else { 
      count++; 
      if (callback !== undefined && callback !== null) 
       callback(undefined); // not sure if you want undefined or "" in this case 
     } 
    }); 
} 

编辑:您可以使用异步回调与QUnit。如上所述,只需使用assert.async()here

QUnit.test("cn_jokes", function(assert) { 
    var done = assert.async(); 
    var jokesDone = 0; 
    var numJokes = 8; // make sure to change this if you add more 
    function joke(n, expected) { 
     jokeGet(n, function(j) { 
      assert.equal(j, expected); 
      if (++jokesDone == numJokes) done(); 
     } 
    } 
    joke(1, "Chuck Norris uses ribbed condoms inside out, so he gets the pleasure."); 
    joke(8, undefined); 
    joke(163, "Ninjas want to grow up to be just like Chuck Norris. But usually they grow up just to be killed by Chuck Norris."); 
    joke(221, "Chuck Norris is the only person to ever win a staring contest against Ray Charles and Stevie Wonder."); 
    joke(352, "Chuck Norris doesn't see dead people. He makes people dead."); 
    joke(502, "Chuck Norris insists on strongly-typed programming languages."); 
    joke(526, "No one has ever pair-programmed with Chuck Norris and lived to tell about it."); 
    joke(598, "Once Chuck Norris and Superman had a competition. The loser had to wear his underwear over his pants."); 
}); 
+0

是否没有办法让jokeGet()函数本身返回值? –

+0

@JMG不,不是真的。你可以让'jokeGet'返回一个你用'data.value.joke'解决的'Promise',并用'await'在'async'函数中调用它。这可能会让你的代码看起来更加结构化,但对于像这样简单的东西来说,使用'await'看起来有点矫枉过正,IE并不支持它。 –

+0

@JMG刚刚注意到你的编辑。我会更新我的答案。 –

0

尝试使用里面的return语句,如果块

if(condition){ 
return str; 
} else { 
return 
} 

我想这品行是由于JS的异步性质,GET请求完成

+0

无效。我猜是因为jokeGet()里面的函数(数据)。使用该策略,变量“str”被认为是不属于jokeGet()的变量,而是属于与ajax调用相关的内部函数。 –

+0

嗯,你是对的,那么你应该使用回调 –

相关问题