繁体   English   中英

将AJAX GET响应的一部分(以JSON格式)存储到字符串变量中的问题

[英]Issues in storing a portion of an AJAX GET response (in JSON) into a string variable

我试图通过AJAX将GET请求的结果的一部分存储到字符串变量中时遇到问题。

基本上,我想这样做,以便某个包含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;
}

我正在请求将信息存储在JSON树中的API。 这是此类树的两个示例:

{ "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()函数,特别是要返回笑话值。 请让我知道这是否可能。

$.getJSON是异步的; 发出请求后,您的代码将继续运行。 因此,在传递给getJSON的回调运行之前,很早就返回了str 您可能应该让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()如提及在这里

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.");
});

尝试在if块内使用return语句

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

我猜这是由于js的异步特性引起的,它在get请求完成之前返回值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM