为什么我的json返回false?

时间:2018-07-10 13:53:48

标签: javascript php json ajax

我需要一些帮助,我不明白为什么我的json返回false ... 这是我的Ajax:

var xhr10 = getXhr();
length=document.getElementById('ChoosenStuff').options.length;
ListChoosenStuffNames = [];
for(var i=0;i<length;i++)
{
    ListChoosenStuffNames[i] = document.getElementById('ChoosenStuff').options[i].value;
}

xhr10.onreadystatechange = function(){
    if(xhr10.readyState === 4 && xhr10.status === 200){
        Selection = JSON.parse(xhr10.responseText); 
        for(var i = 0; i < Selection.length; i++) { 
            document.getElementById('ShowResult').innerHTML += Selection[i] + "\n\r";
        }
    }
};
xhr10.open("POST","Ajax/AjaxGetDTravauxCorrespondant.php",true);
xhr10.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr10.send("ListChoosenStuffNames="+ListChoosenStuffNames);

然后是带有查询的php文件,该文件可在phpMyAdmin上运行:

include '../Reglage/ConnexionBDD.php';
$res = [];

    $Query="Select NomDomaineTravaux from equipement 
    JOIN typeequipement ON equipement.CodeTypeEquipement = 
    typeequipement.CodeTypeEquipement 
    JOIN domainetravaux ON typeequipement.IDDomaineTravaux = 
    domainetravaux.IDDomaineTravaux
    WHERE NomEquipement=:stuff";
    $rep = $bdd->prepare($Query);
    $custom = $_POST['nomsEquipementsChoisis'];
    $rep->bindParam(':stuff',$custom);
    $rep->execute();
    $NomDomaineTravaux= $rep->fetch(PDO::FETCH_ASSOC);
    $res[$i]= $NomDomaineTravaux;
echo json_encode($res, true);

我有{NomDomaineTravaux:“ Divers”} 我如何使用obj = JSON.parse(xhr.reponseText)在我的js文件中显示“ Divers”?

我尝试了obj.NomDomaineTravaux,但是它说他不认识这个...:(

非常感谢

0 个答案:

没有答案