Jump to content

Ajax not working in IE , why?


DeanWhitehouse

Recommended Posts

This is my JS file

// JavaScript Document

function createhandler() 
{
var xmlhttp;

if (window.XMLHttpRequest) 
{
  xmlhttp = new XMLHttpRequest();
}

else if (window.ActiveXObject) 
{
  xmlhttp =  new ActiveXObject("Msxml2.XMLHTTP");
}
return xmlhttp;
}


function chatarea()
{	
var xmlhttp=createhandler();

xmlhttp.onreadystatechange=function () 
{ 
	if(xmlhttp.readyState==4)
	{

	document.getElementById("chatroom").innerHTML=xmlhttp.responseText;

	xmlhttp.onreadystatechange = null;

	xmlhttp.abort();

	}	
};

	xmlhttp.open('GET', 'chat.php', true);

	xmlhttp.send(null);

	var t = setTimeout("chatarea()",1000);

	return true;
}

function addchat()
{
var xmlhttp=createhandler();
xmlhttp.onreadystatechange=function () 
{ 
	if(xmlhttp.readyState==4)
	{

		document.getElementById("error").innerHTML=xmlhttp.responseText;

		xmlhttp.onreadystatechange = null;

		xmlhttp.abort();

	}

};

var message = document.getElementById('chatenter').value;

var params = 'message='+message;

xmlhttp.open('POST', 'createchat.php', true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.setRequestHeader("Content-length", params.length);

xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(params);   

return true;	

}

 

And the problem is in IE, it will still insert the chat message but wont load the new posts , any ideas why, it works fine in Firefox,Chrome and i think Safari.

 

Any help would be good.

 

Using Chromes javascript debugger , i get this when i post .

 

JavaScript Debugger

attached to Chat Room

"Refused to set unsafe header Content-length," source:  (1)

"Refused to set unsafe header Connection," source:  (1)

 

Any one know what it means?

Link to comment
https://forums.phpfreaks.com/topic/122549-ajax-not-working-in-ie-why/
Share on other sites

I use something like this, seems to work in most browsers:

<script type="text/javascript">
function newXHRO()
{
  try { return new XMLHttpRequest(); } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

function someAjax()
{
  var XHRO = new newXHRO();
  var url = 'somefile.php';
  
  XHRO.onreadystatechange=function()
  {
    if(XHRO.readyState==4 && XHRO.status==200)
    {
      alert(XHRO.responseText);
    }
  }
  
  XHRO.open("GET",url,true);
  XHRO.send(null);

}
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.