Link to home
Start Free TrialLog in
Avatar of carljokl
carljokl

asked on

AJAX XMLHttpRequest does not retrieve any content from XML source on FireFox yet works fine on IE

I am having difficulty retrieving XML content using an XMLHttpRequest from Mozilla FireFox. I have a php page which generates XML to be retrieved by a XMLHttpRequest. I have had cross browser success in retriving plain text. However IE retrieves the XML content successfully without any problems. In the case of FireFox not only is the responseXML not set but neither is the responseText ruleling out the possibility of just parsing the text into XML.

I wonder if there is a known problem or issue with retrieving XML using FireFox and any possible workarounds.

The JavaScript for the AJAX should be found under www.jokl.co.uk/scripts/commonfunctions.js
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

It is normally IE that has the problems...

Is the server you retrieve the data from the same as where the script to retrieve it comes from?
Same domain, port, same protocol
Avatar of carljokl
carljokl

ASKER

Yes it is all the same. I have done some further testing.

IE and Opera work ok
FireFox and Netscape don't work (not surprising being both the Mozilla engine)
Safari (3 beta for windows) does not work either.

I think Opera uses the IE engine these days. Perhaps then IE is the odd one out in working when the others do not.

I will explain what I am trying to achieve. I am hoping to enhance my website which only consists of a few pages anyway with a common basic template and only part of the page which changes between pages on the site. It makes sense then in that case if possible that instead of reloading the whole page to just replace the part of the page which is different.

I know my page is hardly the most exiting to begin with but it should be a good learning experience anyway.

All my pages are PHP pages. They are set up to look for the presence of a parameter whether GET or POST which is ajax=true. When this parameter is supplied instead of returning the page as xhtml it returns XML.

This is achieved by choosing which head and foot to attach to any given page. For xhtml the whole page is built into a xhtml page by adding the head.php and foot.php pages. For XML the xmlhead.php and xmlfoot.php are attached instead which uncludes setting the content type header to text/xml.  I am starting to wonder if the problem is more likely to be the content generated by the page being wrong yet I am able to quite happily view the XML in FireFox if I access the content manualy adding the ajax=true parameter.

It could well end up being a really simple problem but I can't see what is wrong.
Any errors in the error console?

No actuall errors are produced but examining of the XMLHttpRequest shows that both the responseXML and responseText are undefined.

It is a shame I don't have a debugger for Mozilla JavaScript to view the state of the internals of the objects and step through the script etc. With Internet Explorer I can debug the script in Visual Studio and see exactly what is going on.
You said, you can see the xml if you load the url directly, but do you set the header text/xml in the php?
Are you using true or false in async?

I have set the the header response type to text/xml and async is set to true:

Here is an example of the php.

the xmlhead.php file:

<?php header("Content-type: text/xml"); ?>
<?xml version="1.0" encoding="UTF-8"?>
<response>
<title><?php echo($title); ?></title>
<content>

the contact.php page which I am currently testing with:

<?php
  require('./functions/custom_functions.php');
  $centreColumns = 7;
  $title = 'Contact';
  if (isAJAXMode())
  {
    require('xmlhead.php');
  }
  else
  {
    require('head.php');
  }
?>
  <tr>
    <td class="vertical" rowspan="1" colspan="1"></td>
    <td class="content" rowspan="1" colspan="<?php echo($centreColumns); ?>">
      <h1>Contact Details</h1>
      <h2>Telephone</h2>
      <p>Tel: (01745) 360912<br />Mob: 07792633993</p>
      <h2>Email</h2>
      <a href="mailto:Carl@Jokl.co.uk">Carl@Jokl.co.uk</a>
      <h2>Mail</h2>
      <p>Carl Jokl<br />Flat 7<br />38 Bath Street<br />Rhyl<br />Denbigshire<br />LL18 3LU<br />Wales</p>
    </td>
    <td class="vertical" rowspan="1" colspan="1"></td>
  </tr>
<?php
  if (isAJAXMode())
  {
    require('xmlfoot.php');
  }
  else
  {
    require('foot.php');
  }
 ?>

The xmlfoot.php file:

</content>
</response>
<?php ?>

The last php tag did have some code in I was trying to use to fix this problem and could be taken out.

The custom functions.php file is purely a set of functions for use in my website:

<?php

function openStandardHTMLPage()
{
  print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
  print("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n");
}

function closeStandardHTMLPage()
{
  print("</html>");
}

function createStandardHeader($title, $styleSheet = './styles/default.css')
{
  print("<head>\n");
  print("<title>" . $title . "</title>\n");
  print("<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $styleSheet . "\" />\n");
  print("</head>\n");
}

function throwDie()
{
  return rand(1, 6);
}

function isAJAXMode()
{
  $ajaxMode = $_REQUEST['ajax'];
  if ($ajaxMode)
  {
    if (strcmp($ajaxMode, 'true') == 0)
    {
      return 1;
    }
  }
  return 0;
}

function getServiceResults()
{
  $serviceQuery = 'SELECT service, description FROM pricing;';
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($serviceQuery))
  {
    return $resultSet;
  }
  else
  {
    return null;
  }
}

function getPriceResults()
{
  $pricingQuery = 'SELECT service, price, rate FROM pricing;';
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($pricingQuery))
  {
    return $resultSet;
  }
  else
  {
    return null;
  }
}

function getEightBallMessages()
{
  $magicEightBallQuery = 'SELECT id, message FROM magic8ball;';
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($magicEightBallQuery))
  {
    return $resultSet;
  }
  else
  {
    return null;
  }
}

function getEightBallMessage($idToGet)
{
  $getMessageQuery = "SELECT id, message FROM magic8ball WHERE id = $idToGet;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($getMessageQuery))
  {
    return $resultSet;
  }
  else
  {
      return null;
  }
}

function addEightBallMessage($newMessage)
{
  $addQuery = "INSERT INTO magic8ball (message) VALUES ('$newMessage');";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($addQuery);
  }
}

function editEightBallMessage($idToEdit, $newMessage)
{
  $editQuery = "UPDATE magic8ball SET message = '$newMessage' WHERE id = $idToEdit ;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($editQuery);
  }
}

function deleteEightBallMessage($idToDelete)
{
  $detetionQuery = "DELETE FROM magic8ball WHERE id = $idToDelete ;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($detetionQuery);
  }
}

function getCookieFortunes()
{
  $fortuneCookieQuery = 'SELECT id, message FROM fortunecookie;';
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($fortuneCookieQuery))
  {
    return $resultSet;
  }
  else
  {
    return null;
  }
}

function getCookieFortune($idToGet)
{
  $getMessageQuery = "SELECT id, message FROM fortunecookie WHERE id = $idToGet;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www') && $resultSet = mysql_query($getMessageQuery))
  {
    return $resultSet;
  }
  else
  {
    return null;
  }
}

function addCookieFortune($newFortune)
{
  $addQuery = "INSERT INTO fortunecookie (message) VALUES ('$newFortune');";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($addQuery);
  }
}

function editCookieFortune($idToEdit, $newFortune)
{
  $editQuery = "UPDATE fortunecookie SET message = '$newFortune' WHERE id = $idToEdit ;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($editQuery);
  }
}

function deleteCookieFortune($idToDelete)
{
  $detetionQuery = "DELETE FROM fortunecookie WHERE id = $idToDelete ;";
  $databaseConnection = mysql_connect('localhost:3306', 'www', 'www');
  if ($databaseConnection && mysql_select_db('www'))
  {
    mysql_query($detetionQuery);
  }
}

?>
I have decided to do a little reorganising to make things simpler.

I have eliminated the xmlhead.php and xmlfoot.php pages and instead intergrated them into the head.php and foot.php pages respectively. This eliminates testing whether AJAX mode is set on each page.

I have also split the custom_functions.php file into a number of smaller files with different functions in order that only the relevant functions for a given page are loaded.

The XML request is still not working. It makes me wonder if others have encountered a similar problem before.
Nothing I do seems to make any difference. It is really frustrating
I have solved this problem now myself. I am not sure exactly what did it as I did some refactoring folowing and example in the book "AJAX in Action" and after that it worked. I think it may be something to do with closing the request before using it.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I could just award you the points anyway as I have unlimited points on my subscription. You did try to help.
thanks - sorry I could not...