【问题标题】:Disable JSON auto merging data with same ID禁用 JSON 自动合并具有相同 ID 的数据
【发布时间】:2023-03-04 19:25:01
【问题描述】:

我对 PHP Select 请求有一个简单的 ajax 调用,如下所示:

 $.ajax({
    type: "POST",
    url: "/renforts/_getIntervenantManager",
    data: {
        IDMission : IDMission,
        IDManager : IDManager
    },
    dataType : 'json',
    global: false,
    success: function(respond) 
    {         
        console.log(respond);
    }
 });

我的 PHP 文件看起来像:

$CUIDManager = $_POST['IDManager'];
$IDMission = $_POST['IDMission'];
$pdo_sql_renforts = DB_renforts();
$sql= "SELECT [IDIntervenant], [week], [hours], [year] FROM  ....";  
$requete = $pdo_sql_renforts->prepare($sql);
$requete->execute(array($IDMission, $IDMission,$CUIDManager));
$tab = $requete->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($tab);

到目前为止一切都很好,因为我得到了回应。

我现在的问题是我的 sql 选择返回类似:

IDIntervenant week hours year
1 22 16 2021
1 23 16 2021
1 24 16 2021
2 22 16 2021
3 25 16 2021

但是我的 JSON 响应是合并具有相同 ID 的行,并在第一次出现这样的 ID 时创建奇怪的数组:

0 : {
    "CUIDIntervenant": "1",
    "week": [
        [
            "22",
            "23"
        ],
        "24"
    ],
    "hours": [
        [
            "16",
            "16"
        ],
        "16"
    ],
    "year": [
        [
            "2021",
            "2021"
        ],
        "2021"
    ],
}
1 : {
    "CUIDIntervenant": "1",
    "week": "23",
    "hours": "16",
    "year": "2021"
}
2 : {
    "CUIDIntervenant": "1",
    "week": "24",
    "hours": "16",
    "year": "2021"
}
3 : {
    "CUIDIntervenant": "2",
    "week": "22",
    "hours": "16",
    "year": "2021"
}
4 : {
    "CUIDIntervenant": "3",
    "week": "25",
    "hours": "16",
    "year": "2021"
}

而我想要的是:

0 : {
    "CUIDIntervenant": "1",
    "week": "22",
    "hours": "16",
    "year": "2021"
}
1 : {
    "CUIDIntervenant": "1",
    "week": "23",
    "hours": "16",
    "year": "2021"
}
2 : {
    "CUIDIntervenant": "1",
    "week": "24",
    "hours": "16",
    "year": "2021"
}
3 : {
    "CUIDIntervenant": "2",
    "week": "22",
    "hours": "16",
    "year": "2021"
}
4 : {
    "CUIDIntervenant": "3",
    "week": "25",
    "hours": "16",
    "year": "2021"
}

【问题讨论】:

  • 那么,这是把事情搞砸的 php json 吗?
  • @vanowm 不,我不这么认为,当我没有在我的 ajax 中设置 `dataType : "json" ` 时,我得到一个与我想要的非常相似的字符串对象
  • 这是不可能的。 fetchAll() 不会创建这样的嵌套对象和数组。
  • 只需在 devtools 中查看您的 ajax 调用的原始响应数据
  • 你也可以在你的php中尝试print_r($tab);,直接在浏览器中打开

标签: javascript php sql json ajax


【解决方案1】:

json 是正确的,jquery 内部发生了一些事情,把它搞砸了。尝试将其作为文本接收并手动转换:

$.ajax({
  type: "POST",
  url: "/renforts/_getIntervenantManager",
  data: {
    mission: IDMission,
    IDManager: IDManager
  },
  dataType: 'text',
  global: false,
  success: function(respond) {
    console.log("original response");
    console.log(respond);
    console.log("JSON");
    console.log(JSON.parse(respond));
  }
});

【讨论】:

  • 哦,是的,你是对的,它解决了我的问题,你知道我可以改变什么来解决那个 Jquery 问题吗?
  • dataType: 'json' 只是让它自动调用JSON.parse()。应该和你在这里做的没什么区别。
猜你喜欢
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多