0

When synchronizing ajax requests, example:

$.when(
    $.get("/api/foo", { prm: 1 }),
    $.get("/api/bar", { prm: 2 })
).done(function (data1, data2) {
    doStuff(data1[0].Value, data2[0].Value);
});

I am getting result objects from the deferred $.get call, that are different to what I am getting when simply calling $.get:

$.get("/api/foo", { prm: 1 }, function (data) { doStuff(data.Value); });

Basically the information that I need is at index 0 of the objects passed to done() (and there is a "success" string at index 1 and raw data at index 2).

I would like to know when and how the result object is being altered this way and if it is safe to always look out for the data at index 0.

4

1 に答える 1

1

これがいつ機能するかなので、data[0] に依存することは完全に安全です。単一の get 呼び出しは次のようになります。

$.get("/api/foo", { prm: 1 }, function (data, statusText, jqXHR) { doStuff(data.Value); });

しかし、他のパラメーターを省略することを選択しましたが、これで問題ありません。これで、jQuery.when を使用して data[0] に強制的にアクセスする理由が理解しやすくなりました... data[0] は get 結果の最初のパラメーターであり、data[1] はステータス テキストであり、data[2] はjqXHR。

于 2013-11-07T16:34:55.003 に答える