Analysis of the sequence of returned data in Ajax request

Time:2020-10-26

This paper analyzes the order of the returned data of Ajax request. For your reference, the details are as follows:

After Ajax requests a URL, the array is in the following format after PHP back-end processing:

Copy codeThe code is as follows:
$a = array( ‘-1’=> 10 ,’-3′ => 2, ‘0’ => ‘5’ ,’-2′ => 4);

Then use the asort function of PHP to sort the array in ascending order by value, as follows:

Copy codeThe code is as follows:
$a = array(‘-3’ => 2, ‘-2′ => 4,’0’ => ‘5’, ‘-1’=> 10  );

The return value received by the front-end AJAX is still unordered.

The possible causes are: because the key value is character change, JS reordered the data

The treatment scheme is as follows:


$i = 0;
foreach ($data as $k => $v) {
   $tmp[$i]['data'] = $v;
   $tmp[$i]['key'] = $k;
   $i++;
}

The data is as follows:


{
  "rows": [
    {
      "data": "2",
      "key": 0-3
    },
    {
      "data": "4",
      "key": -12
    },
    {
      "data": "5",
      "key": 0
    },
    {
      "data": "10",
      "key": -1
    }
  ]
}

Ajax receiving and processing, the data is correct.

I hope this article will be helpful to everyone’s Ajax programming.