2d数组在ajax调用中崩溃


2d array falls apart in ajax call

我在JS中有一个2D数组。

我在ajax之前提醒数组,数组完好无损。

当我ajax它时,我会得到一个错误,所以我现在已经将数组ajax到了一个只运行var转储的页面,并且数组在执行到一半时会尾部脱落,并丢失结构和数据

js 在这里

  $.ajax({
       type: "POST",            
       url: "ajax/updateElements.php",          
       data: {outerarray:outerarray},                       
       success: function(data){
          alert("data="+data);
        },
            error: function(data){
               alert("error");
    }
     });    

php

 <?php
   var_dump($_POST['outerarray']);
 ?>

以及返回的数据

         data=array(40) {
          [0]=>
          array(20) {
            [0]=>
            string(2) "47"
            [1]=>
            string(2) "46"
            [2]=>
            string(2) "-1"
            [3]=>
            string(3) "100"
            [4]=>
            string(2) "35"
            [5]=>
            string(5) "shape"
            [6]=>
            string(190) "shape"
            [7]=>
            string(1) "0"
            [8]=>
            string(1) "0"
            [9]=>
            string(5) "solid"
            [10]=>
            string(3) "000"
            [11]=>
            string(1) "0"
            [12]=>
            string(0) ""
            [13]=>
            string(1) "1"
            [14]=>
            string(2) "st"
            [15]=>
            string(3) "206"
            [16]=>
            string(2) "54"
            [17]=>
            string(1) "0"
            [18]=>
            string(1) "1"
            [19]=>
            string(1) "F"             
          }...until
          [12]=>
          array(20) {
            [0]=>
            string(2) "33"
            [1]=>
            string(3) "179"
            [2]=>
            string(3) "376"
            [3]=>
            string(3) "578"
            [4]=>
            string(3) "203"
            [5]=>
            string(4) "text"
            [6]=>
            string(933) "text"
            [7]=>
            string(2) "11"
            [8]=>
            string(1) "1"
            [9]=>
            string(5) "solid"
            [10]=>
            string(3) "000"
            [11]=>
            string(1) "0"
            [12]=>
            string(0) ""
            [13]=>
            string(1) "1"
            [14]=>
            string(0) ""
            [15]=>
            string(3) "206"
            [16]=>
            stri

这就是它退出的原因

有线索吗?一个星期以来,我一直在为这个而头疼

PDO代码在这里

      $arraycount=count($_POST['outerarray']);
            for($m=0; $m<$arraycount; $m++){            
                $box_id = $_POST['outerarray'][$m][0];
                $top= $_POST['outerarray'][$m][1];
                $left  = $_POST['outerarray'][$m][2];
                $width = $_POST['outerarray'][$m][3];
                $height = $_POST['outerarray'][$m][4];
                $type = $_POST['outerarray'][$m][5];
                $content = str_replace("'n", "<br>",$_POST['outerarray'][$m][6]);   
                $zindex = $_POST['outerarray'][$m][7];
                $borderwidth = $_POST['outerarray'][$m][8];
                $borderstyle = $_POST['outerarray'][$m][9];
                $bordercolor = $_POST['outerarray'][$m][10];
                $padding = $_POST['outerarray'][$m][11];
                $weblink = $_POST['outerarray'][$m][12];
                $opacity = $_POST['outerarray'][$m][13];
                $shapetype = $_POST['outerarray'][$m][14];
                $page_ref = $_POST['outerarray'][$m][15];
                $site_ref = $_POST['outerarray'][$m][16];
                $deletedelement = $_POST['outerarray'][$m][17];
                $allpages = $_POST['outerarray'][$m][18];
                $pageSection = $_POST['outerarray'][$m][19];    
                $stmt = $db->prepare("SELECT * FROM site_content WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                $stmt->execute(array(':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                $row_count = $stmt->rowCount(); 
                if($row_count==0){  
                $stmt = $db->prepare("INSERT INTO site_content(page_ref, site_ref, box_type, box_id, box_top, box_left, box_width, box_height, content, weblink , zindex, border_width, border_colour, border_style, padding, image_opacity, shapetype, allPages, pageSection) VALUES(:field1,:field2,:field3,:field4,:field5,:field6,:field7,:field8,:field9,:field10,:field11,:field12,:field13,:field14,:field15,:field16,:field17,:field18,:field19)");             
                    $stmt->execute(array(':field1' => $page_ref, ':field2' => $site_ref, ':field3' => $type, ':field4' => $box_id, ':field5' => $top, ':field6' => $left, ':field7' => $width, ':field8' => $height, ':field9' => $content, ':field10' => $weblink, ':field11' => $zindex, ':field12' => $borderwidth, ':field13' => $bordercolor, ':field14' => $borderstyle, ':field15' => $padding, ':field16' => $opacity, ':field17' => $shapetype, ':field18' => $allpages, ':field19' => $pageSection));
                    $affected_rows = $stmt->rowCount();                 
                    if($affected_rows !=0){         
                        echo "<br>content added succesfully";
                    }else{
                        echo "<br>no content added";
                    }
                }else{  
                    if($deletedelement=="1"){
                        $stmt = $db->prepare("DELETE FROM site_content WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                        $stmt->execute(array(':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                        $stmt->execute();
                        $affected_rows = $stmt->rowCount();
                        if($affected_rows !=0){         
                            echo "<br>content deleted succesfully";
                        }else{
                            echo "<br>no content deleted";
                        }   
                    }else{
                        if($pageSection==''){ $pageSection="C";}     
                        $stmt = $db->prepare("UPDATE site_content SET box_type=:box_type, box_top=:box_top, box_left=:box_left, box_height=:box_height, box_width=:box_width, content=:content, weblink=:weblink, zindex=:zindex, image_opacity=:image_opacity, border_width=:border_width, border_colour=:border_colour, border_style=:border_style, padding=:padding, shapetype=:shapetype, allPages=:allPages, pageSection=:pageSection WHERE site_ref=:site_ref AND page_ref=:page_ref AND box_id=:box_id");
                        $stmt->execute(array(':box_type' => $type, ':box_top' => $top, ':box_left' => $left, ':box_height' => $height, ':box_width' => $width, ':content' => $content, ':weblink' => $weblink, ':zindex' => $zindex, 'image_opacity' => $opacity, ':border_width' => $borderwidth, ':border_colour' => $bordercolor, 'border_style' => $borderstyle, 'padding' => $padding, 'shapetype' => $shapetype, 'allPages' => $allpages, 'pageSection' =>$pageSection, ':site_ref' => $site_ref, ':page_ref' => $page_ref, ':box_id' => $box_id));
                        $affected_rows = $stmt->rowCount();                        
                        if($affected_rows !=0){         
                            echo "<br>content updated succesfully";
                        }else{
                            echo "<br>no content updated";
                        }
                    }
                }                       
            }

好的,很明显,$.ajax()调用中outerarray数据对象的序列化有问题(可能是旧的jQuery版本?)。为了避免任何问题,您可以将数据编码为JSON字符串,并在PHP脚本中对其进行解码,基本上如下所示:

JS:

$.ajax({
    type: "POST",
    url: "ajax/updateElements.php",
    data: {outerarray: JSON.stringify(outerarray)},
    success: function(data) {
        alert("data="+data);
    },
    error: function(data){
        alert("error");
    }
});

PHP:

$outerarray = json_decode($_POST['outerarray']);
...
$arraycount = count($outerarray);
for ($m=0; $m<$arraycount; $m++) {            
    $box_id = $outerarray[$m][0];
    $top = $outerarray[$m][1];
    ...

在php.ini设置中检查post_max_size,并将其与post 的大小进行比较