php - Ajax + morris.js : Cannot call method 'match' of undefined

标签 php jquery ajax charts morris.js

我正在尝试使用 ajax 和 morris.js 创建图表,但出现此错误:

无法调用 morris-0.4.3.min.js:1 中未定义的方法“匹配”

实际上,图表并没有显示出来。

这是我的代码:

index.html


<link rel="stylesheet" href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.3.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){    
  $.ajax({    
    url: 'db.php', //le fichier qui va nous fournir la réponse      
    success: function(outEvoVal) {    
        console.log(outEvoVal);

        Morris.Line({  
        element: 'graph', // l'élément qui va centenir le dessin  
        data: outEvoVal, //notre objet de données    
        xkey: 'd', // on souhaite afficher notre graph en fonction de temps alors on assigne  'd' pour les axes des x   
        ykeys: ['i','q'],   // les autres sur les axes des y  
        labels: ['chaine','prea'], // ajouter un label a chaque courbe    
        lineColors: ['#0000FF','#044c29'], // différencier chaque courbe avec une couleur    
        lineWidth: 2, // largeur de chaque ligne  
      });    
    }    
  });
});
</script>
</head>
<body>
  <div id="graph"></div>
</body>

db.php(我不应该叫它 db,而是 w/e)

<?php
    /*Mysql hostname*/
    $hostname = 'localhost';
    /*Mysql username*/
    $username = 'root';
    /*Mysql password*/
    $password = 'root';

try {
    $db = new PDO("mysql:host=$hostname;dbname=kpi", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}

 catch(PDOException $e) {
 echo "erreur: ".$e->getMessage();
 }

//requete qui va compter le nombre d’utilisateur ,les question et les reponses en fonction de la date
$requete = $db->query('SELECT Heure, TAT_CHAINE, TAT_PREA FROM out_evo_tat group by Heure');

//faire une boucle pour récupérer le résultat de notre requête
$requete->fetch(PDO::FETCH_ASSOC);
while( $row = $requete->fetch()) {
    $tableau[]=array('d'=>$row['Heure'],'i'=>$row['TAT_CHAINE'],'q'=>$row['TAT_PREA']);
}

//afficher le tableau sous format json
echo json_encode($tableau);
?>

实际上,一切都应该完美运行,因为 console.log(outEvoVal); 显示了正确的字符串。

谢谢大家。

最佳答案

只需将脚本更改为

$(document).ready(function(){    
  $.ajax({    
    url: 'db.php',
    dataType: "json",
    success: function(outEvoVal) {    
      console.log(outEvoVal);
      Morris.Line({  
        element: 'graph',
        data: outEvoVal,
        xkey: 'd',
        ykeys: ['i','q'],
        labels: ['chaine','prea'],
        lineColors: ['#0000FF','#044c29'],
        lineWidth: 2,
      });    
    }    
  });
});

关于php - Ajax + morris.js : Cannot call method 'match' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17944434/

相关文章:

php - HTML 页面中的 JSON 数据在表中

c# - 将 C# ExtNet DirectMethod 实现转换为 VB.NET

php - php如何向数据库中插入多条数据

php - Paypal API : Invalid request parameter

javascript - 与使用 document.getElementsByClassname 相比,在 javascript 中递归降低 DOM 是否效率低下?

java - 在 jquery ajax 调用 jax-rs(resteasy) 中显示错误

jquery - 使用ajax jquery从python Bottle检索数据

javascript - 从 Javascript 作为 base64 调用加载图像

php - 是否可以阻止用户下载 SQLite 文件?

php - str_replace 为数组