Home > Web Front-end > JS Tutorial > Analysis of the order of data returned by ajax request

Analysis of the order of data returned by ajax request

亚连
Release: 2018-05-24 11:38:49
Original
2351 people have browsed it

This article mainly introduces the order of data returned by ajax request, and analyzes the ordering of ajax request return value in the form of examples. Friends in need can refer to it

This article analyzes the order of ajax request with examples The order of returning data is an issue. Share it with everyone for your reference, the details are as follows:

ajax requests a url, and after PHP backend processing, the array is in the following format:

$a = array( '-1'=> 10 ,'-3' => 2, '0' => '5' ,'-2' => 4);
Copy after login

Then use PHP's asort function to sort the array according to value After sorting in ascending order, it is as follows:

$a = array('-3' => 2, '-2' => 4,'0' => '5', '-1'=> 10  );
Copy after login

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

Possible reasons are: because the key value is character replacement, js reorders the data

The solution is as follows:

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

The data at this time is as follows:

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

ajax is received and processed, and the data is correct.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax implements page loading and content deletion

Implements ajax pop-up login function in ECSHOP

Ajax verification for duplicate implementation code

##

The above is the detailed content of Analysis of the order of data returned by ajax request. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template