I am trying to use ajax to display an rss feed. It works fine in IE, but does not work in firefox. Researching, I have heard that firefox has an error that occurs on an asynchronous ajax call. Does anyone know a work around for firefox. Any help much appreciated.

<body>

<script language="javascript" type="text/javascript">

	var xmlHttp;
		try
  			{    			
  			xmlHttp=new XMLHttpRequest();			
  			}
		catch (e)			
  			{  
  			 try
    			{    
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	
    			}
  			catch (e)
    			{    
    			try
      				{      
      				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
      				}
    			catch (e)
      				{      
      				alert("No AJAX support!");       		     
      				}    
      			}  
      		}
	
     
	xmlHttp.onreadystatechange=function()
    	{     
    	if(xmlHttp.readyState==4)
      		{
      		var rss_response=xmlHttp.responseText;
      		
			try 
	  			{
	  			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  			xmlDoc.async="false";
	  			xmlDoc.loadXML(rss_response);								
	  			}
			catch(e)
	  			{
	  			try 
	  				{
	  				parser=new DOMParser();
	  				xmlDoc=parser.parseFromString(rss_response,"text/xml");	
								
	  				}
	  				catch(e)
	  					{
	  					alert(e.message);  					
	  					}
				}	
				
				
				for (loop=1; loop<=10; loop++)
					{
					document.write("<p><a href=\"" + xmlDoc.getElementsByTagName("link")[loop].childNodes[0].nodeValue + "\">")
					document.write(xmlDoc.getElementsByTagName("title")[loop].childNodes[0].nodeValue)
					document.writeln("</a><br>");					
					document.writeln(xmlDoc.getElementsByTagName("description")[loop].childNodes[0].nodeValue + "</p>");					
					}	
      	
      		}
    	}
    var url = ""
  	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);    
  	

</script>

</body>

Thanks,

Gil

Dani AI

Generated

This is very unlikely to be an inherent Firefox bug. When an XHR works in Internet Explorer but not in Firefox the usual causes are the same-origin policy (cross-domain requests), running the page from file:// instead of http://, or side-effects from using document.write() inside an async callback. ’s factory for building the XHR object is a good cross‑browser technique, but it won’t bypass browser security rules.

Quick troubleshooting checklist

  • Check the browser Console and Network panel for errors such as “Cross-Origin Request Blocked”, “Permission denied”, or a failed HTTP status. The Network monitor shows whether the GET was actually issued and what headers/status came back. See Network Monitor and the XHR guide on MDN: Using XMLHttpRequest.
  • Confirm the response Content-Type and that the XML is well-formed. If responseXML is null but responseText has data, the server may be sending the wrong MIME type or the feed may contain stray bytes before the XML prolog. See responseXML behavior: XMLHttpRequest.responseXML.

Practical fixes

  • If the RSS is on another host, fetch it from your server (same origin) and expose it to the page. Example simple PHP proxy:
    <?php
    header('Content-Type: application/rss+xml');
    echo file_get_contents('http://www.kbtx.com/home/headlines/index.rss');

    Or enable CORS on the feed server (add Access-Control-Allow-Origin); see CORS.

  • Avoid document.write() in an async callback — it can clobber the page or behave inconsistently. Build nodes and append them or set innerHTML on a container instead. Also iterate item elements (e.g., item) and use zero‑based indexes rather than assuming tag lists line up.

If the page is being opened locally, serve it from a small HTTP server (for example, python -m http.server) and retest; that will quickly reveal whether the problem is origin/security related.

Recommended Answers

All 2 Replies

It's not a Firefox bug its that you're only coding for IE. Use this to get your request object

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

Then just

var xmlHttp = createXMLHTTPObject();

Thanks for your answer. However, I've run some tests and it does create the request object in firefox.

This statement creates the firefox obj: xmlHttp=new XMLHttpRequest();

The problem is on the readystate function call. Firefox will not call the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.