jQuery的:$ .getJSON排序的Chrome浏览器/ IE浏览器中的数据?器中、浏览器、数据、getJSON

2023-09-11 01:13:25 作者:别碰我的人生

我通过使用Ajax和jQuery的$ .getJSON其正常读取数据和prepared对象接收它的关联数组(ID => VAL)。有,但是,很烦人的排序问题。

I'm passing an associative array (id => val) using Ajax and receiving it with jQuery's $.getJSON which read the data properly and prepared the object. There is, however, very annoying sorting issue.

看来,在Chrome和IE浏览器中的数据变成由副阵列的ID部分排序。因此,如果阵列应该是(5 =>'XXX',3 =>'FFF),它实际上变成(3 =>​​FFF,5 =>'XXX')。在Firefox中正常工作,即不进行排序。

It appears that on Chrome and IE the data becomes sorted by the id part of the associate array. So if the array should be (5=> 'xxx', 3 => 'fff') it actually becomes (3 => 'fff',5=> 'xxx'). On FireFox it works as expected, i.e. not sorted.

任何想法?

推荐答案

似乎是最好的方法是避免关联数组的。当你想发送的联系人阵列只会将其发送作为两个单独的阵列 - 一键以及价值观之一。这里的PHP code做到这一点:

Seems the best way is to avoid associative arrays at all. When you want to send an associate array simply send it as two separate arrays - one of keys and one of values. Here's the PHP code to do that:

    $arWrapper = array();
    $arWrapper['k'] = array_keys($arChoices);
    $arWrapper['v'] = array_values($arChoices);
    $json = json_encode($arWrapper);

和简单的JavaScript code做任何你想用它

and the simple JavaScript code to do whatever you'd like with it

            for (i=0; i < data['k'].length; i++) {
                console.log('key:' + data['k'][i] + ' val:' + data['v'][i]);
            }