devknob Posted December 10, 2009 Share Posted December 10, 2009 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>"; ?> Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted December 10, 2009 Share Posted December 10, 2009 you might want to try reponseText first, because you xml is not well formed, it doesn't have a root element. the responseText will show you the text returned so you can trouble shoot Quote Link to comment Share on other sites More sharing options...
devknob Posted December 10, 2009 Author Share Posted December 10, 2009 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; } Quote Link to comment Share on other sites More sharing options...
devknob Posted December 10, 2009 Author Share Posted December 10, 2009 the xml is validated. ive tried some things people claim work in multiple browsers and nothing does. Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted December 10, 2009 Share Posted December 10, 2009 Have you thought of going to JQuery to do ajax> Quote Link to comment Share on other sites More sharing options...
devknob Posted December 10, 2009 Author Share Posted December 10, 2009 lol Well since that seems the only way to get anything done, thats what im going to use. how lame, I wanted to do my own ajax, but there doesnt seem to be working examples, or im just missing something stupid. Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted December 11, 2009 Share Posted December 11, 2009 fine 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.