Jump to content

responseXML always returns null or undefined


devknob

Recommended Posts

spent hours trying to figure this out. Everyone on google swears this is content type, but i doubt it since im setting the correct content type. Anyone know why this doesnt work? Im pretty new to actual ajax. What. All I want to do is create xml in php, then pass xml values back to javascript and within the innerhtml of a div tag.

 

html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script type='text/javascript' src='ajax.js'></script>
    <title>PHP AJAX Example</title>
  </head>
  <body>
    <input type='button' onclick='MakeRequest("meowmix");' value='Use AJAX!!!!'/>
    <div id='ResponseDiv'>
      This is a div to hold the response.
    </div>
    <div id='OtherDiv'>8</div>
  </body>
</html>

 

javascript:

function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Please enable Javascript or update your browser to use this function")
        return false;
      }
    }
  }
  return xmlHttp;
}



function MakeRequest(i_id)
{
  
  var xmlHttp = getXMLHttp();
  var xmlDoc;
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlDoc.test);

}
  }

  xmlHttp.open("GET", "ajax.php?id=" + i_id, true);
  xmlHttp.setRequestHeader("Content-Type", "text/xml");
  xmlHttp.send(null);

  xmlDoc=xmlHttp.responseXML;

}

function HandleResponse(response)
{

  document.getElementById('ResponseDiv').innerHTML = response;

}

 

 

php:

<?
header("Content-type: text/xml"); 
header('Cache-Control: no-cache');
header('Pragma: no-cache');


echo "<test>".$_GET['id']."</test><rawr>rawrs23</rawr>";

?>

Link to comment
Share on other sites

responseText has been working fine

 

<?
header("Content-type: text/xml"); 
header('Cache-Control: no-cache');
header('Pragma: no-cache');

echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
echo "<root><test>".$_GET['id']."</test><rawr>rawrs23</rawr></root>";

?>

 

That still doesnt work. I just need to know how to specify that I only want the value within the xml tag.

 

 

js also changed a bit, still no results:

function MakeRequest(i_id)
{
  
  var xmlHttp = getXMLHttp();
  var xmlDoc;
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {

      HandleResponse(xmlDoc.getElementsByTagName("test")[0].nodeValue);

}
  }

  xmlHttp.open("GET", "ajax.php?id=" + i_id, true);
  xmlHttp.setRequestHeader("Content-Type", "text/xml");
  xmlHttp.send(null);

  xmlDoc=xmlHttp.responseXML.documentElement;

}

 

 

Link to comment
Share on other sites

fine  :D

I used to do ajax manually until I heard about JQuery

 

here is a sample of the code that I used to use

 

<script>
var xhttp=false;
var isIE=false;

function makeAjaxCall(data) {

  if (document.all) { isIE=true;}

      if (isIE) 
			{ 
			if (window.XMLHttpRequest)
				{
			          // If IE7, Mozilla, Safari, etc: Use native object
			          xhttp = new XMLHttpRequest()
				}
				else
				{
				if (window.ActiveXObject)
					{
			          // ...otherwise, use the ActiveX control for IE5.x and IE6
			          xhttp = new ActiveXObject("Msxml2.XMLHTTP");
			    }
     			}
      	} 
			else 
			{ 
        xhttp = new XMLHttpRequest();
      	}
      // set the event handler
      xhttp.onreadystatechange = ajaxReturn;
      // prep the call, http method=POST, true=asynchronous call
      var rankArgs = 'function='+data;
      xhttp.open("POST", "http://www.domain.com/ws/ws_webservicename.php", true);
      
      xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      // send the call with args
      xhttp.send(rankArgs);
}

function ajaxReturn()
{
        
  if (xhttp.readyState==4) {

    //responseXML contains an XMLDOM object
    var x = xhttp.responseXML.getElementsByTagName("root");
    var nodes = x[0].getElementsByTagName("test")[0];
	if (nodes.childNodes[0])
		{		
  	  var test = nodes.childNodes[0].nodeValue;
           var rawr = x[0].getElementsByTagName("rawr")[0];
  			}

}
</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.