【问题标题】:$.each() loop through json not looping$.each() 循环通过 json 不循环
【发布时间】:2014-09-20 22:16:20
【问题描述】:

我有一个 ajax 调用返回 json 我正在尝试将返回的项目发送到特定的输入文本 ID。

这是ajax:

$.ajax({
    url: "php/myfirstfile.php",
    type: "POST",
    data: $("#frameroof").serialize(),
    cache: false,
    dataType: "json",
    success: function (json) {
        $.each(json, function () {
            $.each(json, function (key, value) {
                /// do stuff
                $('#' + key ).val(value);
            });
        });
    }
});

这是返回的内容:[{"a-frame":"100"}][{"vertical":"350"}]

当我需要一个循环时,它看起来我得到了 2 个数组。我不确定。

这里是php

if(isset($_POST["cart"])){
    $frameArray = ($_POST["cart"]);

    if(is_array($frameArray)){
        foreach($frameArray as $row){
            $catalogue = $row['catalogue'];
            $certification = $row['certification'];
            $catagory =  $row['catagory'];
            $subcatagory =  $row['subcatagory'];
            $length = $row['length'] ;

            $sql = "SELECT `price` AS  '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND  `certification` = '$certification' AND `catagory` = '$catagory' AND  `sub_catagory` = '$subcatagory' AND `length` = '$length' ";

            $result = $short_connect->query($sql);

            if (($result) && ($result->num_rows > 0)) {
                $results = array();
                //convert query result into an associative array
                while ($row = $result->fetch_assoc()) {
                    $results[] =  $row;
                }

                //dump all data from associative array converted from query result
                echo (json_encode($results,true));

                $result->free();
            }
        }
    }
}

$short_connect->close();

【问题讨论】:

  • [{"a-frame":"100"}][{"vertical":"350"}] 作为字符串不是有效的 JSON
  • 我认为您正在尝试遍历字符串。
  • 是否有可能看到你的一些 PHP 的样子?
  • 那不是 JSON。要么你试图手动输出 JSON(你不应该这样做)并且正在破坏它,要么你的服务器代码运行了两次。无论哪种方式,我们都需要查看服务器端代码。
  • 这里是php代码

标签: php jquery ajax json


【解决方案1】:

我相信你的问题很简单。在循环内部,您正在初始化结果数组并输出它。这会导致创建一个新的结果数组,将其序列化为 JSON,并为循环的每次迭代输出。因此,您向浏览器发送的不是 JSON,而是几块 JSON 一起运行。

这是您需要做的基本想法:

<?php
// Initialize the output data
$results = array();

foreach($something as $a_something) {
    $results[] = do_something_to($a_something);
}

//Serialize and send the output data
echo (json_encode($results,true));

考虑到该模式重新排列您的代码会产生这个(这应该适合您,如果您的if 条件不满足或查询不返回任何内容,它将向浏览器返回一个空数组):

<?php

// Initialize the output data
$results = array();

if(isset($_POST["cart"])){
    $frameArray = ($_POST["cart"]);

    if(is_array($frameArray)){
        foreach($frameArray as $row){
            $catalogue = $row['catalogue'];
            $certification = $row['certification'];
            $catagory =  $row['catagory'];
            $subcatagory =  $row['subcatagory'];
            $length = $row['length'] ;

            $sql = "SELECT `price` AS  '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND  `certification` = '$certification' AND `catagory` = '$catagory' AND  `sub_catagory` = '$subcatagory' AND `length` = '$length' ";

            $result = $short_connect->query($sql);

            if (($result) && ($result->num_rows > 0)) {
                //convert query result into an associative array
                while ($row = $result->fetch_assoc()) {
                    //Add to the output data
                    $results[] =  $row;
                }

                $result->free();
            }
        }
    }
}

$short_connect->close();

//Serialize and send the output data
//dump all data from associative array converted from query result
echo (json_encode($results,true));

【讨论】:

  • 你的编码员。我真的很感激你比你知道的更多
  • 没问题!继续插电并在这里互动 - 你会继续变得更好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2012-12-08
  • 2019-12-19
相关资源
最近更新 更多