watagal Posted June 11, 2008 Share Posted June 11, 2008 This JS routine is called from an HTML control using the 'onclick' event: all good here function getEmailData(id,el) { ajaxCall(PHPDIR+'form_MyPage_getEmailData.php', "POST", 'id='+id+'&el='+el, async, getEmailData_Callback, 'xml');} The PHP (form_MyPage_getEmailData.php) produces the following XML (responseXML) code: all good here <?xml version="1.0" encoding="ISO-8859-1"?><root> <node> <target>divContent</target> <id>1</id> <label>Administrator</label> <email>admin@************.com</email> <isPrimary>1</isPrimary> <share>All</share> </node></root> This is the 'stateChanged' or callback routine from the ajax call above: The error I get is "arr[0]" has no properties (var target = 3rd line down) - so my "newXmlDoc() function (way below) receives an xml file (per W3school) as an argument.Here I'm not specifying a file, but the 'responseXML' (in memory) - which is why 'arr[]' is empty. How do I fix this? thanks, Gal function getEmailData_Callback(responseXML) { var xmlDoc= newXmlDoc(responseXML); // (forms+ajax.js) var arr = xmlDoc.getElementsByTagName("node"); var target = arr[0].getElementsByTagName("target")[0].childNodes[0].nodeValue; if (target == '') { // setValue('id',"txtEmailLabel", responseXML.getElementsByTagName("label")[0].childNodes[0].nodeValue); var email = responseXML.getElementsByTagName("email")[0].childNodes[0].nodeValue.split("@"); setValue('id',"txtEmailAddress1", email[0]); setValue('id',"txtEmailAddress2", email[1]); setChecked('id',"chkEmailIsPrimary",responseXML.getElementsByTagName("isPrimary")[0].childNodes[0].nodeValue); setChecked('id','radEmailShare'+ responseXML.getElementsByTagName("share")[0].childNodes[0].nodeValue,true); } else { // List Email Addresses on leftside list (accordion menu) var html = '<table>'; for (var i=0; (i < arr.length); i++){ html += '<tr><td><div class="contactInfo" onclick="java script:getEmailData('; html += arr[i].getElementsByTagName("id")[0].childNodes[0].nodeValue+',\'\');">'; html += '<b>'+arr[i].getElementsByTagName("label")[0].childNodes[0].nodeValue+'</b><br />'; html += space(4); html += arr[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; html += '<br /><br /></div></td></tr>'; } html += '</table>'; setInnerHTML('id',target,html); }} XML Function: function newXmlDoc(doc) { // per W3school try { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //Internet Explorer } catch(e) { try { xmlDoc = document.implementation.createDocument("","",null); //Firefox, Mozilla, Opera, etc. } catch(e) { alert(e.message) } } try { xmlDoc.async=false; xmlDoc.load(doc); // i.e. "books.xml" } catch(e) { alert(e.message) } return xmlDoc;} Link to comment Share on other sites More sharing options...
jesh Posted June 11, 2008 Share Posted June 11, 2008 function getEmailData_Callback(responseXML) { var xmlDoc= newXmlDoc(responseXML); // (forms+ajax.js) var arr = xmlDoc.getElementsByTagName("node"); var target = arr[0].getElementsByTagName("target")[0].childNodes[0].nodeValue; ... What do you get if you alert the responseXML variable? Does it show you the XML?Also, if your PHP file is setting the Content-Type header to indicate that the response coming back to the browser is XML, there shouldn't be any need execute your newXmlDoc function because the responseXML property of the XMLHttpRequest is already an XMLDocument. The forum won't let me link to the page on the w3schools site ("You have entered a link to a website that the administrator does not allow links to"), so check out the section of the AJAX tutorial called "AJAX ResponseXML". Link to comment Share on other sites More sharing options...
watagal Posted June 12, 2008 Author Share Posted June 12, 2008 Thanks jesh-I tried the alert() and it came up null - but according to Firebug, the response has XML data:I'm looking at w3schools example: http://www.w3schools.com/ajax/tryit.asp?fi...httprequest_js4Here's my ajaxCall() function: // create AJAX objectvar xmlHttp = null;function createAjaxObject() { try { xmlHttp=new XMLHttpRequest();} // Firefox, Opera 8.0+, Safari catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} // Internet Explorer catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");} } return xmlHttp;}// AJAX callfunction ajaxCall(dataSource, method, encode, async, callback, response) { xmlHttp = createAjaxObject(); if (xmlHttp != null) { xmlHttp.onreadystatechange = function() { if ((xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" ) && xmlHttp.status == 200) { if (response == 'text' && callback) { callback(xmlHttp.responseText); } if (response == 'xml' && callback) { callback(xmlHttp.responseXML); } } } if (method == "POST") { xmlHttp.open("POST", dataSource, async); xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xmlHttp.send(encode+"&sid="+Math.random()); } else { xmlHttp.open("GET", dataSource+"&sid="+Math.random(), async); xmlHttp.send(null); } } else alert ("Browser does not support HTTP Request");} Here's the trigger: function getEmailData(id,el) { // Select item from leftside list & get data ajaxCall(PHPDIR+'form_MyPage_getEmailData.php', "POST", 'id='+id+'&el='+el, async, getEmailData_Callback, 'xml');} Here's the response code: - responseXML still comes in null. function getEmailData_Callback() {alert(xmlHttp.responseXML);// var xmlDoc = newXmlDoc(responseXML); // (forms+ajax.js) var arr = xmlHttp.responseXML.documentElement.getElementsByTagName("node"); var target = arr[0].getElementsByTagName("target")[0].childNodes[0].nodeValue; if (target == '') { // setValue('id',"txtEmailLabel", responseXML.getElementsByTagName("label")[0].childNodes[0].nodeValue); var email = responseXML.getElementsByTagName("email")[0].childNodes[0].nodeValue.split("@"); setValue('id',"txtEmailAddress1", email[0]); setValue('id',"txtEmailAddress2", email[1]); setChecked('id',"chkEmailIsPrimary",responseXML.getElementsByTagName("isPrimary")[0].childNodes[0].nodeValue); setChecked('id','radEmailShare'+ responseXML.getElementsByTagName("share")[0].childNodes[0].nodeValue,true); } else { // List Email Addresses on leftside list (accordion menu) var html = '<table>'; for (var i=0; (i < arr.length); i++){ html += '<tr><td><div class="contactInfo" onclick="java script:getEmailData('; html += arr[i].getElementsByTagName("id")[0].childNodes[0].nodeValue+',\'\');">'; html += '<b>'+arr[i].getElementsByTagName("label")[0].childNodes[0].nodeValue+'</b><br />'; html += space(4); html += arr[i].getElementsByTagName("email")[0].childNodes[0].nodeValue; html += '<br /><br /></div></td></tr>'; } html += '</table>'; setInnerHTML('id',target,html); }} Link to comment Share on other sites More sharing options...
jesh Posted June 12, 2008 Share Posted June 12, 2008 I tried the alert() and it came up null - but according to Firebug, the response has XML data:Here's the response code: - responseXML still comes in null.function getEmailData_Callback() {alert(xmlHttp.responseXML);// var xmlDoc = newXmlDoc(responseXML); // (forms+ajax.js)} I'm guessing, since your first posting had getEmailData_Callback(responseXML) rather than getEmailData_Callback(), that this is just a typo in the forum.What do you get if you do this:// there may be something weird happening with naming the // parameter "responseXML". Try something like "response".function getEmailData_Callback(response){ alert("xmlHttp.responseXML=\n" + xmlHttp.responseXML); alert("response parameter =\n" + response);} Link to comment Share on other sites More sharing options...
watagal Posted June 12, 2008 Author Share Posted June 12, 2008 Thanks again jesh- in either case - i get nullI think the problem is either my XmlHttpRequest or my PHP script generating the XML response - because my response is returning via the "responseText", not the "responseXML".Here's my 'responseXML':Here's my 'responseText':So where might I be going wrong?Gal Link to comment Share on other sites More sharing options...
jesh Posted June 13, 2008 Share Posted June 13, 2008 I think the problem is either my XmlHttpRequest or my PHP script generating the XML response - because my response is returning via the "responseText", not the "responseXML".I can only think of two reasons why the responseXML would be null. The first would be because the XML is malformed If the XML is bad, the responseXML property will be null. You can test it by opening the document in your browser. Firefox, at least, will inform you if the XML isn't formed properly. But, based on what you posted, your XML looks fine.The second would be that the server is not setting the Content-Type for the XML correctly. It needs to be set to "text/xml" rather than "text/plain" or "text/html". If the webserver is not telling the browser that the content is "text/xml", then the responseXML property will be null. Link to comment Share on other sites More sharing options...
watagal Posted June 16, 2008 Author Share Posted June 16, 2008 Thanks again jesh -It appears (to me) that I am requesting a "text/xml" content type, but recieving "text/html" type (via firebug):So my XML response must be malformed, but I don't know how or where? Here it is: <?xml version="1.0" encoding="ISO-8859-1"?><root> <node> <target></target> <id>1</id> <label>Administrator</label> <email>admin@abc.com</email> <isPrimary>1</isPrimary> <share>All</share> </node> <node> <target></target> <id>3</id> <label>Family</label> <email>gal@abc.com</email> <isPrimary>0</isPrimary> <share>Family</share> </node></root> Where can I get the rules for writing XML responses? Or is there a setting in Apache?Thanks so much for the time you're putting into this, Gal Link to comment Share on other sites More sharing options...
jesh Posted June 16, 2008 Share Posted June 16, 2008 It appears (to me) that I am requesting a "text/xml" content type, but recieving "text/html" type (via firebug):I just read today in the PHP forum that PHP sends the responses back using "text/html" by default. If you want PHP to send back "text/xml", you have to explicitly tell your PHP to do so. I'm so out of practice with PHP that I won't even begin to attempt an example. In ASP.NET (C#), it'd look something like this:Response.ContentType = "text/xml"; This page might help you with PHP:http://www.php.net/header Link to comment Share on other sites More sharing options...
watagal Posted June 16, 2008 Author Share Posted June 16, 2008 Thankyou!! That's it!!my reponse PHP file includes the header() AND IT WORKS!! -- Thanks sooo much! header('Content-type: text/xml');echo '<?xml version="1.0" encoding="ISO-8859-1"?>'."\n"; Link to comment Share on other sites More sharing options...
jesh Posted June 16, 2008 Share Posted June 16, 2008 Excellent! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.