The order problem analysis of the return data of Ajax request _ajax related

Source: Internet
Author: User

This article analyzes the order of the return data of Ajax requests. Share to everyone for your reference, specific as follows:

When Ajax requests a url,php backend processing, the array is formatted as follows:

Copy Code code as follows:
$a = Array ('-1 ' =>, '-3 ' => 2, ' 0 ' => ' 5 ', '-2 ' => 4);

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

Copy Code code as follows:
$a = Array (' 3 ' => 2, '-2 ' => 4, ' 0 ' => ' 5 ', '-1 ' => 10);

The return value that the front-end Ajax receives is still unordered.

Possible causes are: Because the key value is a character swap, JS to the data to reorder processing

The processing programme is as follows:

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

At this point the data is as follows:

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

Ajax receive processing, the data is correct.

I hope this article will help you with Ajax programming.

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.