0

I'm using jquery $.when to call a REST url which returns json.

When I execute $.when with a single parameter I get different object in data1 result than when I execute multiple ones, even though return parameters should all be independent.

<script>
$(document).ready(function () {

    fun1(1025);
    fun2(1025);

    function fun1(id) {
        $.when(_restFun1(id), _restFun1(id)).done(function(data1, data2) {
            console.log(data1);
        });
    }

    function fun2(id) {
        $.when(_restFun1(id)).done(function(data1) {
            console.log(data1);
        });
    }

});
</script>

the objects printed to console.log are different even though the two functions should print the same object!.

NOTE:

the _restFun1 function is something like this:

function _restFun1(id)
{
    return $.ajax({
        url: "http://192.123.12.3/test.php?id="+id,
        data: "",
        dataType: 'json',
        success: function (data1) {
        }
    });
}

which returns a json object, in one case I get the json object as it is, in the case of multiple calls I get an object with multiple other fields like responseText, responseJSON, a "success" string and then in an array the real JSON thing I needed.

dendini
  • 3,842
  • 9
  • 37
  • 74

1 Answers1

2

Note, not appear entirely clear at jQuery.when() documentation as to returned jQuery.ajax call returning array of [responseText, textStatus, jqxhr] ("success") , or [jqxhr, textStatus, errorThrown] ("error")

If a Deferred resolved to a single value, the corresponding argument will hold that value. In the case where a Deferred resolved to multiple values, the corresponding argument will be an array of those values.

closest example demonstration could find at documentation

var d1 = new $.Deferred();
var d2 = new $.Deferred();
var d3 = new $.Deferred();

$.when( d1, d2, d3 ).done(function ( v1, v2, v3 ) {
    console.log( v1 ); // v1 is undefined
    console.log( v2 ); // v2 is "abc"
    console.log( v3 ); // v3 is an array [ 1, 2, 3, 4, 5 ]
});

d1.resolve();
d2.resolve( "abc" );
d3.resolve( 1, 2, 3, 4, 5 ); // returned as `array` at `.done()`

See also

Ajax response has different format when used with JQuery when function

jquery when documentation for response and errors


Response would be an array [responseText, textStatus, jqxhr], where responseText response from server; textStatus success, error; jqxhr jQuery jqXHR object

Try

function fun1(id) {
  $.when(_restFun1(id), _restFun1(id))
  .done(function(data1, data2)) {
    // `data[0]`, `data1[0]` should be `responseText` , `responseJSON`    
     console.log(data1[0], data2[0]);  
  })
}
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • The console log returns my desired object, but why is it contained in a new array named 0?? where is it documented? and why this behaviour only when multiple calls are passed? – dendini Mar 27 '15 at 14:55
  • See http://stackoverflow.com/questions/11456946/ajax-response-has-different-format-when-used-with-jquery-when-function , http://stackoverflow.com/questions/17181839/jquery-when-documentation-for-response-and-errors , – guest271314 Mar 27 '15 at 15:00
  • Ok found the answer thanks to your two links, if you could elaborate a bit on your answer I could accept it. – dendini Mar 27 '15 at 15:06