Analysis of the order of returned data in ajax requests,
This article analyzes the order of data returned by ajax requests. We will share this with you for your reference. The details are as follows:
Ajax requests a url. After php backend processing, the array format is as follows:
Copy codeThe Code is as follows: $ a = array ('-1' => 10,'-3' => 2, '0' => '5 ', '-2' => 4 );
Then, use the php asort function 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 returned values received by front-end ajax are still unordered.
The possible cause is that js re-sorts the data because the key value is a character change.
The solution 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 receives and processes the data correctly.
I hope this article will help you with ajax programming.
Articles you may be interested in:
- In jquery, how does ajax return results rather than callback is executed in the same order?
- Returned custom error information in the Jquery ajax execution sequence (instance description)
- The execution sequence of code outside the Jquery $. ajax Function
- How to Set synchronization for ajax function execution sequence in jquery
- Parse the call sequence of ajax events
- Analysis of Ajax execution sequence flow and callback Problems
- Jquery $. ajax event execution sequence