Jump to content

ajax


abdelelbouhy

Recommended Posts

hello guys i'm trying to use a web service which should return xml document when i get the response as responseText it shows the document but when i used responseXML and try to get the elements to display its values the returned document seems to be empty and here is the codevar xhr = false;if(window.XMLHttpRequest){ xhr = new XMLHttpRequest();}else if(window.ActiveXObject){ xhr = window.ActiveXObject('msxml2.XMLHTTP');}function getDoc(data){ xhr.abort(); xhr.open('GET','ajax4.php?symbol=' + data,true); xhr.onreadystatechange = getData; xhr.send(null);}function getData(){ if(xhr.readyState == 4 && xhr.status == 200){ var response = xhr.responseXML; var xmlDoc = null; var xmlObject = null; if(window.ActiveXObject){ xmlDoc = response.childNodes[1].firstChild.nodeValue; xmlObject = new ActiveXObject('microsoft.XMLDOM'); xmlObject.async = 'false'; xmlObject.loadXML(response); alert(xmlObject); }else{ xmlDoc = response.childNodes[0].firstChild.nodeValue; var parser = new DOMParser(); xmlObject = parser.parseFromString(xmlDoc,'text/xml'); alert(xmlObject); } }}/*php code */<?php header('content-type: text/xml'); ?> <?php $url = "http://www.webservicex.net/stockquote.asmx/GetQuote?symbol="; $rq = $_GET['symbol']; $url .= $rq; $url = DOMDocument::load($url); echo $url->saveXML(); ?>i think the problem is xmlDoc variable which it should contain the element value but i got error of undefined instead

Link to comment
Share on other sites

If responseText contains a value that looks like XML as a string, but responseXML does not contain an XML object, then the XML may be malformed in some way. In particular, I think (not sure) that some browsers may object if the document does not have a line like this at the top:<?xml version="1.0" encoding="UTF-8"?>But other problems can occur, such as unclosed tags, incorrectly nested tags, incorrectly nested or escaped quotation marks, etc.Is an XML object constructed in other browsers but not IE? Or does every browser have the same problem?

Link to comment
Share on other sites

If responseText contains a value that looks like XML as a string, but responseXML does not contain an XML object, then the XML may be malformed in some way. In particular, I think (not sure) that some browsers may object if the document does not have a line like this at the top:<?xml version="1.0" encoding="UTF-8"?>But other problems can occur, such as unclosed tags, incorrectly nested tags, incorrectly nested or escaped quotation marks, etc.Is an XML object constructed in other browsers but not IE? Or does every browser have the same problem?
IE gives error but firefox doesn't give any error nor doesn't show anything
Link to comment
Share on other sites

Try alert(response) just to see if your browsers are parsing the document correctly. (The value alerted should be something like "xml object".) If it is correct, try alert(response.childNodes[0].firstChild.nodeValue) to see what that gives you. It might be nothing. Remember that if your document is formatted with tabs and line breaks, the first child of an element might be a text node, and a text node can have no child nodes of its own. It is usually more reliable to identify XML nodes using element.getElementsByTagName.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...