【问题标题】:Trouble passing array of JSON objects to PHP将 JSON 对象数组传递给 PHP 时遇到问题
【发布时间】:2018-08-28 13:38:20
【问题描述】:

以下循环遍历表的几行,并在进行时收集数据。 对于每次迭代,我都会说 $jrow = JSON.stringify ($rowdata) 然后 $jschedule.push ($jrow);

在字符串化之后,一行看起来像这样...... {"count":1,"bmaclass":"18-007","customer":"CST4070","course":"CRS0001","evdate":"","time":"","location" :"0","stu1":"STU2336","stu2":"STU2623","stu3":"STU6808","instructor":"0","evaluator":"0"}

我希望这会产生一个字符串数组,然后我将其字符串化并传递给 PHP;然而 PHP 什么也看不到。我不知道如何创建这个数组并将其传递给 PHP。好像我以前做过,但这次我没有运气。

var $jschedule = [];
$("table.tngdays tr").each (function ($index, $row){
            $this = $(this);
            $this.css("background","orange");

            $time = $this.find('input.time');
            $evtime = $time.val();
            $time.css("background","pink");

            $evdate = $this.find ("input.date").val(),
            $evtime = $this.find ("input.time").val(),
            $bmaclass = $("select.pendingClasses").val()
            $location = $this.find (".loc").val()
            $instructor = $this.find (".ins").val()
            $evaluator = $this.find (".tce").val()

            //console.log ($evdate, $evtime);

            $rowdata = {
               count      : $count,
               bmaclass   : $bmaclass,
               customer   : $result.customer,
               course     : $result.course,
               evdate     : $evdate,
               time       : $evtime,
               location   : $location,
               stu1       : $result.p1,
               stu2       : $result.p2,
               stu3       : $result.p3,
               instructor : $instructor,
               evaluator  : $evaluator
            }
            $count ++;

            $jrow = JSON.stringify($rowdata);

            console.log ("Rowdata = " + typeof($jrow))
            console.log ($jrow);
            $jschedule.push ($jrow);

            })


        }) //each


        $data = {
           events:$jschedule
        }

        $data = JSON.stringify($data);
        console.log ($data);

        $.post ("/Training/ScheduleClass/pdo_saveClassEvents.php",{data:$data}, function ($result){
           console.log ("Events = " + $result);
        })

谢谢, DMD

【问题讨论】:

  • 谢谢,是的,都是错别字。我在粘贴其余代码后输入了该行。我已经修复了我的错误
  • console.log($data) 在 $.post 之前查看它的内容,或检查您的开发者工具网络请求中的请求并查看正在发送的内容。
  • 你是双重编码。只调用一次 JSON.stringify。也尝试不调用 stringify。如果有记忆,jQuery 会自动处理。
  • 如果我不对数据进行字符串化,DEV 工具会显示一个字符串数组。>[ ] 如果我进行字符串化,它会显示一个空数组 [ ]
  • 发现问题。有一组错位的结束标签})。当我在正确的标签内移动东西时,一切都很好。谢谢大家

标签: php jquery arrays stringify


【解决方案1】:

问题是一组错误的结束标签。一旦我将帖子移动到正确的标签内,问题就消失了。

谢谢大家

【讨论】:

    猜你喜欢
    • 2010-10-22
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2015-01-07
    • 2012-12-03
    相关资源
    最近更新 更多