Need to create an array after receiving Ajax

1 option

var q1 = $.ajax({ url: "php/sms_sender.php?act=get_tech", type: "POST", dataType: "json", success: function (data) { } }).then(q1) .then(function (data) { console.log(data) 

getting

 [Object { name="ั‡1", num="33"}, Object { name="ั‡2", num="22"}] 

Option 2

 $(document).ready(function () { var q1 = $.ajax({ url: "php/sms_sender.php?act=get_tech", type: "POST", dataType: "json", }) var q2 = $.ajax({ url: "php/sms_sender.php?act=get_tech2", type: "POST", dataType: "json", }) var q = $.when(q1, q2) .then(function (data, data2) { console.log(data) 

Conclusion:

 [[Object { name="ั‡1", num="33"}, Object { name="ั‡2", num="22"}], "success", Object { readyState=4, responseText="[{"name":"\u04e\u04b\u...","num":"33"}]", status=200, ะตั‰ั‘...}] 

why different response formats?

    1 answer 1

    This is a feature of the when function: each parameter that is forwarded to the then function is what a particular promise resolved.

    If you slightly modify the first option, you can see that the results are the same:

     var q1 = $.ajax({ url: "php/sms_sender.php?act=get_tech", type: "POST", dataType: "json", success: function (data) { } }).then(function (data) { console.log(arguments) }); 

    In order to receive only the first parameter in when , you need to add then to the request declarations:

     var q1 = $.ajax({ url: "php/sms_sender.php?act=get_tech", type: "POST", dataType: "json", }).then(function(data){ return data;}); var q2 = $.ajax({ url: "php/sms_sender.php?act=get_tech2", type: "POST", dataType: "json", }).then(function(data){ return data;}); 

    Now, when executed,

     [Object { name="ั‡1", num="33"}, Object { name="ั‡2", num="22"}]