javascript - Why my code gives me a TypeError: x.documentElement is undefined? -
i'm trying xml php:
<?php // ... $row = mysqli_fetch_array($result); $xml = new domdocument("1.0", "utf-8"); $book = $xml->createelement("book"); $book = $xml->appendchild($book); $bookid = $xml->createelement("id",$row['id']); $bookid = $book->appendchild($id); $bookauthor = $xml->createelement("author",$row['author']); $bookauthor = $book->appendchild($bookauthor); $booktitle = $xml->createelement("title", $row['title']); $booktitle = $book->appendchild($booktitle); $bookgenre = $xml->createelement("genre",$row['genre']); $bookgenre = $book->appendchild($bookgenre); $bookdescription = $xml->createelement("description",$row['description']); $bookdescription = $book->appendchild($bookdescription); echo($xml->asxml()); mysql_close($con); ?>
to js function (using ajax):
function bytitle(title) { document.getelementbyid("results").innerhtml = ""; var xmldoc; var output = ""; if (title != "") { if (window.xmlhttprequest) { xmlhttp = new xmlhttprequest(); } xmlhttp.open("get", "getbooksbytitle.php?title=" + title, true); xmlhttp.send(null); xmlhttp.onreadystatechange = function () { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { xmldoc = xmlhttp.responsexml; var x = xmldoc.getelementsbytagname('book'); alert(x.textcontent); output = "<table><br><p><b>results:</b></p><table><tr><th>id</th><th>author</th><th>title</th><th>genre</th><th>price</th><th>description</th></tr><tr>"; output += "<td>" + x.documentelement.getelementsbytagname('id')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + x.documentelement.getelementsbytagname('author')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + x.documentelement.getelementsbytagname('title')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + x.documentelement.getelementsbytagname('genre')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + x.documentelement.getelementsbytagname('price')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + x.documentelement.getelementsbytagname('description')[0].childnodes[0].nodevalue + "</td></tr></table>"; document.getelementbyid("results").innerhtml = output; } }
but unfortunately typeerror:
x.documentelement undefined (:mozilla).
i've tried lot of different "solutions" none of them make difference...
you don't need call x.documentelement
have xmldoc
should use this:
output += "<td>" + xmldoc.getelementsbytagname('id')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + xmldoc.getelementsbytagname('author')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + xmldoc.getelementsbytagname('title')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + xmldoc.getelementsbytagname('genre')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + xmldoc.getelementsbytagname('price')[0].childnodes[0].nodevalue + "</td>"; output += "<td>" + xmldoc.getelementsbytagname('description')[0].childnodes[0].nodevalue + "</td></tr></table>";
also: easier consume json server response ajax requests, because you'd need use json.parse(data)
use response plain js object.
Comments
Post a Comment