Simple XmlHttpObject problem

This is just a portion of the code I have, let me know if you need more to fully diagnose the problem.

The problem is strange to me because this code was all working fine a couple days ago. I’ve made some minor changes to server configuration but don’t believe i’ve touched any of this code.

When using the code below the alert returns a value of null. In the past this has never happened, it has always returned the xml file I’m requesting. I have noticed a strange difference between browsers. This script is used to fill a form automatically. In its current state it works perfectly in opera, but does not work in firefox or IE (used to work in all the browsers).

I’m by no means a javascript expert, am I missing something here?

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js/entry-info-xml.php";
url=url+"?q="+str;

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  xmlDoc=xmlhttp.responseXML;
alert(xmlDoc);
}
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

That never worked (correctly).

You should only access the responseXML after the response has been received, which is the purpose of the readystatechange event.

Pretty much any ajax tutorial should cover this.

I’m sorry, I tried to include too little code and made a mental error. Take a look at the code again. This is what I have (with the exception of the code that parses the xml file afterward). I’ve now added my stateChange function and moved the alert inside this function (as it is in my code).

I hope this is more helpful.

Ok, I’m quite confused yet. I loaded the script on to my test server and it appears to run just fine. Are there some common server configuration settings in apache that would prevent it from completing the http request?

I’ve verified that the xml file at the variable url is correct and prints out the correct information based on the variable it receives in the url parameter.

Something weird is happening here:

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
if (xmlhttp.status==200){

  xmlDoc=xmlhttp.responseXML;
alert(xmlDoc);
}
}
}

In this example the alert statement always executes meaning the xmlhttp request is ready and has returned a status of “OK”, however the alert prints a value of null despite the url variable in the get statement pointing to a valid xml file.

What confuses me even more is the fact that this works perfectly in opera but works in no other browsers. This leads me to think it is something other than a server configuration error. However, this is conflicted by the fact that moving the script to a different server solves the problem.

Suggestions?

I would make sure:
that url gets served with a proper cntent-type http header, eg text/xml
the xml is truely valid(including the charset)
the browser is not caching a previous, but now fixed bad response.

post that url if needed.

This is the value of the url variable:

js/entry-info-xml.php?q=6286

And this is the dynamic XML file it generates:

<?xml version="1.0" encoding="ISO-8859-1"?><entry><fname>firstname</fname>
<mi></mi><lname>lastname</lname><prefix></prefix><title></title><dept>department</dept>
<organization>organization</organization><address></address><country></country><phone>
</phone><webpage></webpage><expertise>expertise</expertise></entry> 

Does this help at all?

That xml looks ok to me. But, I can’t see the http headers without a url, so you will need to verify.

You should be sending

Content-type: text/xml;charset=iso-8859-1

Thanks for your patience. My inability to allow you to test this on the actual website makes things a little more difficult on both of us. These are the headers that are sent/received in the process of the script. I edited out a few pieces of confidential information but hopefully this is more than enough to help us diagnose this problem. Once again, I really appreciate your help.

Note: You may see some utf-8 encodings too because I just changed this in the last couple of minutes.

http://www.mywebsite.org/who/js/entry-list-xml.php?getNamesByLetters=1&letters=firstname,+lastname&#37;20J.

POST /who/js/entry-list-xml.php?getNamesByLetters=1&letters=firstname,+lastname%20J. HTTP/1.1
Host: www.mywebsite.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) FirePHP/0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://www.mywebsite.org/who/entry.php?u=
Content-Length: 20
Cookie:
Authorization: Basic
Pragma: no-cache
Cache-Control: no-cache
rndval=1266008131319
HTTP/1.1 200 OK
Date: Fri, 12 Feb 2010 20:55:30 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 194
Connection: close
Content-Type: text/html;charset=utf-8

http://www.mywebsite.org/who/js/entry-info-xml.php?q=6284

GET /who/js/entry-info-xml.php?q=6284 HTTP/1.1
Host: www.mywebsite.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) FirePHP/0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.mywebsite.org/who/entry.php?u=
Cookie:
Authorization: Basic

HTTP/1.1 200 OK
Date: Fri, 12 Feb 2010 20:55:32 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 401
Connection: close
Content-Type: text/html;charset=utf-8

http://www.mywebsite.org/who/js/entry-email-xml.php?q=6284

GET /who/js/entry-email-xml.php?q=6284 HTTP/1.1
Host: www.mywebsite.org
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) FirePHP/0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.mywebsite.org/who/entry.php?u=
Cookie:
Authorization: Basic

HTTP/1.1 200 OK
Date: Fri, 12 Feb 2010 20:55:32 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 81
Connection: close
Content-Type: text/html;charset=utf-8


https://server.mywebsite.com:2087/json-api/loadavg

GET /json-api/loadavg HTTP/1.1
Host: server.mywebsite.com:2087
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729) FirePHP/0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: https://server.mywebsite.com:2087/scripts/command
Cookie:
Authorization: Basic

HTTP/1.1 200 OK
X-Keep-Alive-Count: 10
Connection: Keep-Alive
Keep-Alive: timeout=120, max=200
Server: cpsrvd/11.25
Date: Fri, 12 Feb 2010 20:55:33 GMT
Content-Length: 46
Cache-Control: no-cache, no-store, private, must-revalidate
Content-Type: text/plain

Well there you go, you’re sending
Content-Type: text/html;charset=utf-8

Send the right header using php’s header() function

Below is the file that generates the xml file. It has a header call to set it to text/xml. By looking at the headers I’m assuming that this is not working. Do I need to make this header call somewhere other than the xml file?

<?php
/* Generates an XML file containing data about an entry */
/* Used by AJAX to auto-fill application fields */

header("Content-type: text/xml");
require_once('../config.php');

$q = $_GET['q'];

$entry = new Entry($q);
$info = $entry->GetEntry();

echo '<?xml version="1.0" encoding="utf-8"?><entry>';
  echo "<fname>" . $info['fname'] . "</fname>";
  echo "<mi>" . $info['mi'] . "</mi>";
  echo "<lname>" . $info['lname'] . "</lname>";
  echo "<prefix>" . $info['prefix'] . "</prefix>";
  echo "<title>" . $info['title'] . "</title>";
  echo "<dept>" . $info['dept'] . "</dept>";
  echo "<organization>" . $info['organization'] . "</organization>";
  echo "<address>" . str_replace('&', '&amp;', $info['address']) . "</address>";
  echo "<country>" . $info['country'] . "</country>";
  echo "<phone>" . $info['phone'] . "</phone>";
  echo "<webpage>" . str_replace('&', '&amp;', $info['webpage']) . "</webpage>";
  echo "<expertise>" . str_replace('&', '&amp;', $info['expertise']) . "</expertise>";
echo "</entry>";

?>

First you say this is some of the xml
<?xml version=“1.0” encoding=“ISO-8859-1”?>

But looking at that php script…it would produce
<?xml version=“1.0” encoding=“utf-8”?>

Yes I’m sorry for the confusion, i’ve done some debugging between posts here. In the post with the headers you will notice I said:

Note: You may see some utf-8 encodings too because I just changed this in the last couple of minutes.

The reason for this change Is last month I thought I changed all the encodings in this application to utf-8 from iso. Then when debugging this problem I realized I missed this xml file. In order to make it match the rest of the website I just changed it to utf-8 after my initial post.

When you mentioned the content-type was wrong I thought you were point to the text/html instead of text/xml, but were you talking about the differences in encoding type?

I don’t think the encoding is the issue here, but, if it’s utf-8, you should declare it as such. Both in the http header and the xml.

So, your php script attempts to send the content-type as text/xml, but when you check, that header is not actually sent?

I’m trying to understand these headers as well. I’m not sure what headers correspond to what requests.

I ran another test this morning on the headers. Last time I used Live HTTP Headers the Firefox Add-on. This time I used the following code to print the headers.

I added this code to the stateChange function mentioned in previous threads.

var sHeaders = xmlhttp.getAllResponseHeaders();
        alert(sHeaders);

Below is the output:

Output for Opera Browser [Working]:

Date: Sat, 13 Feb 2010 15:55:11 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 401
Connection: close
Content-Type: text/html;charset=utf-8

Output for Firefox [not working]:

Date: Sat, 13 Feb 2010 15:55:25 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 478
Connection: close
Content-Type: text/html;charset=utf-8

Output for IE [not working]:

Date: Sat, 13 Feb 2010 15:55:25 GMT
Server: Apache/2.2.13 (Unix)
X-Powered-By: PHP/5.2.10
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 401
Connection: close
Content-Type: text/html;charset=utf-8

Why would this work in only Opera? That does not make sense to me.

Wow I finally got it working and it was a matter of moving 1 line of code down 4 lines.

This is my original xml generating script

<?php
/* Generates an XML file containing data about an entry */
/* Used by AJAX to auto-fill application fields */

/*header("Content-type: text/xml");*/
require_once('../config.php');

$q = $_GET['q'];

$entry = new Entry($q);
$info = $entry->GetEntry();

header("Content-type: text/xml");

echo '<?xml version="1.0" encoding="utf-8"?><entry>';
  echo "<fname>" . $info['fname'] . "</fname>";
  echo "<mi>" . $info['mi'] . "</mi>";
  echo "<lname>" . $info['lname'] . "</lname>";
  echo "<prefix>" . $info['prefix'] . "</prefix>";
  echo "<title>" . $info['title'] . "</title>";
  echo "<dept>" . $info['dept'] . "</dept>";
  echo "<organization>" . $info['organization'] . "</organization>";
  echo "<address>" . str_replace('&', '&amp;', $info['address']) . "</address>";
  echo "<country>" . $info['country'] . "</country>";
  echo "<phone>" . $info['phone'] . "</phone>";
  echo "<webpage>" . str_replace('&', '&amp;', $info['webpage']) . "</webpage>";
  echo "<expertise>" . str_replace('&', '&amp;', $info['expertise']) . "</expertise>";
echo "</entry>";

?>

All I had to do to get this to work was move the header down a couple of lines. Strange why this was a problem to begin with. Must have been something in the Entry class that was overwriting the header after it was being set.

Thanks for your help crmalibu, it is greatly appreciated. My debugging skills are ok but not great and it always helps to learn new approaches to debugging (especially with technology i’m unfamiliar with).

Ya, either config.php, or your Entry class sends a content-type header, and it was overriding it.