Php 如何在JQuery中解码JSON数组?

Php 如何在JQuery中解码JSON数组?,php,jquery,ajax,Php,Jquery,Ajax,我需要用JSON解码一个数组。我尝试了几种代码,但似乎都不起作用。数组是SQL查询的结果。我需要在一个HTML表中显示结果,但我无法根据我的需要对JSON中的结果进行解码 $retourTitre=null; $retourDesc=null; $retourAppName=null; $retourEnsName=null; $retourProfession=null; $retourYear=null; $retour=array(); 而($dn1=mysqli\u fetch\u数组(

我需要用JSON解码一个数组。我尝试了几种代码,但似乎都不起作用。数组是SQL查询的结果。我需要在一个HTML表中显示结果,但我无法根据我的需要对JSON中的结果进行解码

$retourTitre=null;
$retourDesc=null;
$retourAppName=null;
$retourEnsName=null;
$retourProfession=null;
$retourYear=null;
$retour=array();
而($dn1=mysqli\u fetch\u数组($reqSQL)){
$retourTitre=“”.$dn1[“所有权”];
$retourDesc=”“.$dn1['desc_fr'];
$retourAppName=“”.$dn1['NoApprenti'];
$retourEnsName=“”.$dn1['nomensignant'];
$retourProfession=“”.$dn1[“Profession”];
$retourYear=“”.$dn1[“年];
$retour['Titre']=$retourTitre;
$retour['Desc']=$retourDesc;
$retour['AppName']=$retourAppName;
$retour['EnsName']=$retourEnsName;
$retour['Profession]=$retourProfession;
$retour['Annee']=$retourYear;
echo json_编码($retour);
}
mysqli_close($联络);
函数rechercher()
{
var projet=document.getElementById('search_project')。值;
$.ajax({
类型:“POST”,
url:“requete.php”,
数据:{projet:projet},//Passer la变量JS
成功:功能(结果){//Récupérer la Réponse
resultat=json_解码(resultat);
//画面
$('Title_res')。空();
$('Title_res')。追加(resultat.Titre);
$('#Desc_res').empty();
$('#Desc_res')。追加(resultat.Desc);
$('#appName_res').empty();
$('#appName_res').append(resultat.appName);
$('#ensName_res').empty();
$('#ensName_res').append(resultat.ensName);
$('professional_res').empty();
$('Profession_res')。追加(resultat.Profession);
$('Year'u res')。空();
$('Year'u res')。追加(resultat.Annee);
}, 
}“json”);
}
而不是

 resultat = json_decode(resultat);
试一试


欢迎来到SO。正如@robin singh所提到的,您应该在susses回调中使用,而不是使用php函数,而不是js函数

还有一个简单的例子说明它是如何工作的:

const strJson='{“a”:“str a”,“b”:“str b”}';
const json=json.parse(strJson);
log(json.a);

log(json.b)只需在Ajax中添加
数据类型:'JSON'

$.ajax({
type: "POST",
url: "requete.php",
dataType:'JSON',
data: {projet:projet}, //Passer la variable JS
success: function(resultat){
//Remplir le tableau
$('#Title_res').empty();
$('#Title_res').append(resultat.Titre);
$('#Desc_res').empty();
$('#Desc_res').append(resultat.Desc);
$('#appName_res').empty();
$('#appName_res').append(resultat.AppName);
$('#ensName_res').empty();
$('#ensName_res').append(resultat.EnsName);
$('#Profession_res').empty();
$('#Profession_res').append(resultat.Profession);
$('#Year_res').empty();
$('#Year_res').append(resultat.Annee);
 } 
});
dataType:'JSON'
将响应数据转换为JSON对象


这与JSON.parse(resultat)相同

谢谢您的回答,但它仍然不起作用。我试图发出警报以查看“resultat”的内容,但代码在数据类型行之前或之前的行停止。我可以通过调试控制台确认。 我只是不知道为什么。如果有人给我小费,我就要了

resultat = JSON.parse(resultat); //The code stop here
alert(resultat);

好的,我找到了一个不使用JSON的解决方案。我只返回一个用php生成的表

$retour = null;
while ($row = $reqSQL -> fetch_assoc()) {
    $retour ="<tr>
                <td>".$row['title_fr']."</td>
                <td>".$row['desc_fr']."</td>
                <td>".$row['NomApprenti']."</td>
                <td>".$row['NomEnseignant']."</td>
                <td>".$row['Profession']."</td>
                <td>".$row['year']."</td>   
            </tr>";

    echo $retour; // retour des lignes du tableau
}
$retour=null;
而($row=$reqSQL->fetch_assoc()){
$retour=”
“$row['title\u fr']”
“$行['desc_fr']
“$row['noapprenti']”
“$row['nomensignant']”
“$row['Profession']”
“$行['年]。”
";
echo$retour;//retour des lignes du tableau
}
$retour = null;
while ($row = $reqSQL -> fetch_assoc()) {
    $retour ="<tr>
                <td>".$row['title_fr']."</td>
                <td>".$row['desc_fr']."</td>
                <td>".$row['NomApprenti']."</td>
                <td>".$row['NomEnseignant']."</td>
                <td>".$row['Profession']."</td>
                <td>".$row['year']."</td>   
            </tr>";

    echo $retour; // retour des lignes du tableau
}