首页 > 解决方案 > JSON输入意外结束,找不到原因

问题描述

当我想要我的 ajax 返回时,我很难找到为什么我的 js 文件出现错误:JSON.parse(xhr.responseText)

我有一个检索这些数据的 php 文件:

 [{Duree: "01:00:00"}, {Duree: "00:30:00"}, {Duree: "01:00:00"}, {Duree:"00:30:00"},…]
0:{Duree: "01:00:00"}
    Duree:"01:00:00"
1:{Duree: "00:30:00"}

我找不到为什么我的 json 有意外的结束。这是我的 php 文件:它使用一些查询过滤器,然后循环遍历我的一组对象,然后将每个对象的持续时间添加到我的数组中并对其进行 json_encode。

<?php
session_start();
include '../../Reglage/ConnexionBDD.php';
$Query="SELECT DISTINCT Intervention.IDIntervention,DateFin,Remarque,Intervention.IDAgent,DateDebut,IDTypeTravaux,IDVacation as Vacation, IDEtat as Etat,DateDebut,Duree,DescriptionTache 
FROM intervention 
JOIN interventionmaincourante ON interventionmaincourante.IDIntervention = intervention.IDIntervention 
JOIN participerintervention ON Intervention.IDIntervention=participerintervention.IDIntervention 
JOIN faire_la_vacation ON participerintervention.IDAgent = faire_la_vacation.IDAgent
WHERE DateVacation = intervention.DateDebut  ";//base de la requete
//SOME SQL FILTERS WHICH ADD CLAUSE IN PRINCIPAL QUERY
//MANY SQL FILTERS
$messagesParPage=100;
$sql = "Select count(*) as NB from Intervention";
$rep = $bdd->query($sql);
$total = $rep->fetch();
$total = $total['NB'];
$nombreDePages=ceil($total/$messagesParPage);

if(intval($_POST['CurrentPage']) != 0) {
    if(isset($_POST['CurrentPage'])) {
    $pageActuelle=intval($_POST['CurrentPage']);
    if($pageActuelle>$nombreDePages) { // Si la valeur de $pageActuelle (le numéro de la page) est plus grande que $nombreDePages...
        $pageActuelle=$nombreDePages;
    }
}
else { // Sinon
    $pageActuelle=1; // La page actuelle est la n°1
}
    $premiereEntree=($pageActuelle-1)*$messagesParPage; // On calcul la première entrée à lire
    $Query .= " order by DateDebut DESC, IDIntervention DESC, Vacation LIMIT ". $premiereEntree . "," . $messagesParPage;
}
else {
    $Query .= " order by DateDebut DESC, IDIntervention DESC, Vacation";
}

$currentID = "";
$lastID = "";
$rep = $bdd->query($Query);
$total = array();
$tot = array();
foreach($rep as $Intervention) {
    $idVacation = 0;
    switch ($_POST['Vacation']) {
        case 'Matin' :
            $idVacation = 1;
            break;
        case 'Apres-Midi':
            $idVacation = 2;
            break;
        case 'Nuit':
            $idVacation = 3;
            break;
        default:
            break;
        }
    if ($Intervention['Vacation'] == $idVacation) {
        $Query = 'SELECT Duree FROM fairegammeoperatoire WHERE IDIntervention=:id';
        $rep = $bdd->prepare($Query);
        $custom = $Intervention['IDIntervention'];
        $rep->bindParam(':id',$custom);
        $rep->execute();
        $Duree = $rep->fetch(PDO::FETCH_ASSOC) ;
        array_push($total,$Duree);
    }
}
echo json_encode($total);

我的 JS 代码:

tot = '';
var xhr1 = getXhr();
xhr1.onreadystatechange = function(){
    if(xhr1.readyState == 4 && xhr1.status == 200){
        Selection = xhr.responseText;
        for(let i = 0; i < Selection.length; i++) {
            if (i == 0) {
                start = Selection[i];
            }
            if ((i + 1) < Selection.length) {
                end = Selection[i + 1];
                tot = addTimes(start, end);
                console.log(tot);
            }
            start = tot;
        }

        alert(tot);
    }
};
xhr1.open('POST','maincourante/Ajax/AjaxCalculGammeOperatoire.php',true);
xhr1.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr1.send(Send);

标签: phpjsonajax

解决方案


推荐阅读