Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX/PHP/XML-通过Javascript获取一些数据_Javascript_Php_Ajax_Xml - Fatal编程技术网

AJAX/PHP/XML-通过Javascript获取一些数据

AJAX/PHP/XML-通过Javascript获取一些数据,javascript,php,ajax,xml,Javascript,Php,Ajax,Xml,我的PHP文件通过我的数据库生成XML,这个文件有效,问题在于JavaScript部分 情况:当我键入一封信时,我可以从PHP生成的XML中获取数据 PHP <?php $bdd = new mysqli("localhost", "root", "", "sommets"); if ($bdd->connect_error) {die("Impossible de se connecter");} $bdd->set_charset("utf-8"); if

我的PHP文件通过我的数据库生成XML,这个文件有效,问题在于JavaScript部分

情况:当我键入一封信时,我可以从PHP生成的XML中获取数据

PHP

<?php
  $bdd = new mysqli("localhost", "root", "", "sommets");
  if ($bdd->connect_error) {die("Impossible de se connecter");}
  $bdd->set_charset("utf-8");

  if (isset($_GET['recherche'])) {
     $recherche = addslashes($_GET['recherche']);
     $sql = "SELECT * from sommets where som_nom like '$recherche%'";
     $rec = $bdd->query($sql) or die($bdd->error);
     header("Content-type: text/xml");
     echo("<sommets>");
  while ($row = $rec->fetch_object()) {
      echo("<sommet altitude=\"{$row->som_altitude}\">");
      echo("<nom>{$row->som_nom}</nom>");
      echo("<region>{$row->som_nom}</region>");
      echo("</sommet>");
  }
  echo("</sommets>");  
}
?>
或与“
fistChild
”或“
nodeValue
”相同。我只想从PHP生成的XML中读取一个括号


你有什么想法吗

首先,在使用
xhr.responseXML.getElementsByTagName
时,应使用“sommet”而不是“sommets”

其次,
resultatsXML[i].getElementsByTagName(“nom”)[i]
正在对
resultatsXML
getElementsByTagName
的结果使用变量
i
。如果作为第一点,
resultatsXML[i]
包含一个“sommet”,那么根据您的示例,
getElementsByTagName
将只返回一个XML标记,因此它应该是:

var resultatsXML = xhr.responseXML.getElementsByTagName("sommet");
if(resultatsXML.length > 0){
    for (var i=0; i<resultatsXML.length; i++) {
        var nom_sommet = resultatsXML[i].getElementsByTagName("nom")[0].nodeValue;
        document.getElementById("nom").innerHTML += nom_sommet;
    }
}
var resultatsXML=xhr.responseXML.getElementsByTagName(“sommet”); 如果(resultsxml.length>0){ 对于(var i=0;i 未捕获的TypeError:无法读取未定义的属性“0”

意味着

resultatsXML[i].getElementsByTagName("nom")[i].childNodes
未定义,因为

resultatsXML[i].getElementsByTagName("nom")[i]
不是一棵树加上它,你只有一个值

同时将
[i]
更改为
[0]

resultatsXML[i].getElementsByTagName("nom")[0]
var nom_sommet = resultatsXML[i].getElementsByTagName("nom")[0].nodeValue;
document.getElementById("nom").innerHTML += nom_sommet;
只需从
resultsxml[i].getElementsByTagName(“nom”)[0]立即获取
.nodeValue

resultatsXML[i].getElementsByTagName("nom")[0]
var nom_sommet = resultatsXML[i].getElementsByTagName("nom")[0].nodeValue;
document.getElementById("nom").innerHTML += nom_sommet;

.childNodes[0]
不是树,并且
.childNodes[0].childNodes
未定义,只需在
childNodes[0]
之后立即获取
.nodeValue