Analysis of the order of returned data in ajax requests,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.