dougui5419 2016-04-11 21:39
浏览 62
已采纳

如何在ajax调用的客户端处理多个数组

I need to process 5 arrays in an AJAX call but only two are being decoded. I have been trying to debug this for one and a half days and I cannot work out why. I make the AJAX call with the following code

function postrates(){
  if(changearray.length>0){
     $.ajax({
        type:'POST',
        async:false,
        url:"xml_postdailyrate.php",
        data:{
           'changearray':JSON.stringify(changearray),
           'minarray  ':JSON.stringify(minarray),
           'lockarray ':JSON.stringify(lockarray),
           'costarray ':JSON.stringify(costarray),
           'availarray':JSON.stringify(availarray)
        },
        success:function(JSON){
           changearray=[];
           availarray =[];
           costarray  =[];
           minarray   =[];
           lockarray  =[];
        },
        error:function(){
           alert('Failed');
        }
     })
  }
}

It passes the arrays OK, the following server side code has been stripped down to make my testing easier.

<?php
   header("Content-Type: application/json");
   $changearray="Empty";
   $availarray="Empty";
   $minarray="Empty";
   $lockarray="Empty";
   $costarray="Empty";
   if(isset($_POST['changearray'])){
      $changearray=json_decode($_POST['changearray']);
   }
   if(isset($_POST['availarray'])){
      $availarray=json_decode($_POST['availarray']);
   }
   if(isset($_POST['minarray'])){
      $minarray=json_decode($_POST['minarray']);
   }
   if(isset($_POST['lockarray'])){
      $lockarray=json_decode($lock);
   }
   if(isset($_POST['costarray'])){
      $costarray=json_decode($_POST['lockarray']);
   }
   $arr['changearray'] =$changearray;
   $arr['availarray'] =$availarray;
   $arr['minarray'] =$minarray;
   $arr['lockarray'] =$lockarray;
   $arr['costarray'] =$costarray;
   $arr = json_encode($arr);
   echo("$arr");

Firebug "post" tab displays the 5 arrays correctly as

changearray [63291,63368,63292]
minarray    ["7","9","8"]
lockarray   ["X","X","X"]
costarray   ["4","6","5"]
availarray  ["1","3","222"]

The Firebug "JSON" tab displays

changearray [63291, 63368, 63292]
 0           63291
 1           63368
 2           63292
availarray  ["1", "3", "222"]
 0          "1"
 1          "3"
 2          "222"
minarray    "Empty"
lockarray   "Empty"
costarray   "Empty"

I cannot find out why it does not decode minarray, lockarray, costarray.

Thank you for taking the time to read my problem.

  • 写回答

1条回答 默认 最新

  • douyong1285 2016-04-11 21:45
    关注

    You aren't using the key "minarray", you are using the key "minarray " (note the spaces). The same for the other two arrays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家
  • ¥20 使用Matlab APP设计一个插补小程序
  • ¥15 evo评估时曲线出现问题
  • ¥15 eNSP拓扑图配置实验
  • ¥20 有没有兄弟会替换fmod studio的.bank文件内的音效?
  • ¥20 如何解决访问网址被屏蔽的情况