11 Replies - 10546 Views - Last Post: 11 June 2008 - 10:56 AM

#1 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

responseText is empty in IE

Post icon  Posted 08 June 2008 - 02:51 PM

Hello all,

I'm using AJAX to populate a div tag in my HTML when a drop down list item is selected. It works perfectly in Safari, Firefox, Opera, but fails in IE. The responseText just comes back blank from the php script.

Here's the Javascript it has to deal with:

function GetXmlHttpObject()
{
	xmlHttp = null;
	try
	{
		// Firefox, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // 6.0+
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); // 5.5+
		}
	}
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var response = xmlHttp.responseText;
		document.getElementById("contentBox").innerHTML=response;
	} 
}


And here's the php script I'm using to fetch some data from the MySQL server:
<?php
	$id = $_GET["id"];
	include('connect.php');
	$theQuery = mysql_query("select post from archive where title='" . $id . "'");
	$row = mysql_fetch_assoc($theQuery);
	echo $row['post'];
?>


Is This A Good Question/Topic? 0
  • +

Replies To: responseText is empty in IE

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: responseText is empty in IE

Posted 08 June 2008 - 03:34 PM

I am unsure if you have this anywhere in your code or not, but I can't find it in the JS you provided.

You need something that creates the xmlHTTP object, try this:

var xmlHttp = GetXmlHttpObject();


Here is an example of some ajax that works with IE:

<script>
function createRequestObject(){
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var http = createRequestObject();

function sndReq() {
	http.open('get', 'URL.whatever', true);
	http.onreadystatechange = handleResponse;
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	http.send();
}

function handleResponse() {
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById('respond').innerHTML = response;
	}
	else{
		var response = "Loading -- please wait";
		document.getElementById('respond').innerHTML = response;
	}
}
</script>


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: responseText is empty in IE

Posted 08 June 2008 - 05:42 PM

My apologizes, here's all the code.

var xmlHttp;

function changePost(theid)
{
	GetXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("This should do something other than alert the user.");
		return false;
	}
	
	var url = "php/getPost.php?id=" + theid;
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(null);
}

function GetXmlHttpObject()
{
	xmlHttp = null;
	try
	{
		// Firefox, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // 6.0+
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); // 5.5+
		}
	}
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var response = xmlHttp.responseText;
		document.getElementById("contentBox").innerHTML=response;
	} 
}


The only thing you had that I didn't was:
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");


I added that and it still didn't work. Although I think it might be something to do with how IE parses the response. I think it has something to do with the content-type. I just don't know what to make it.

Anymore ideas?

By the way, here's my website if you want to see what I'm talking about.

This post has been edited by atdrago: 08 June 2008 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: responseText is empty in IE

Posted 08 June 2008 - 09:51 PM

Okay, loking at the code (and splicing it a bit with mine) I got it working in IE6, and then was able to get it back to mostly what you have now. So, here is what I came up with:

function GetXmlHttpObject(){
	xmlHttp = null;
	try
	{
		// Firefox, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // 6.0+
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); // 5.5+
		}
	}
	return xmlHttp;
}

var xmlHttp = GetXmlHttpObject();

function changePost(theid){
	var url = "php/getPost.php?id=" + theid;
	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(null);
}

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		var response = xmlHttp.responseText;
		document.getElementById("contentBox").innerHTML=response;
	} 
}



Hope it works for you :)
Was This Post Helpful? 0
  • +
  • -

#5 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: responseText is empty in IE

Posted 08 June 2008 - 11:47 PM

Darn, I really thought there was going to be something screwy in IE with how (and where) you declare your variables. I guess not.

That said, I copied and pasted the code you posted, tried it in IE 7, and I'm still having the same problem.

Go to my site and see if you have the same problem. Try to select a different article from the archive drop down list. If it's working for you, I'm thinking it might just be an issue with my cache grabbing old data.

Thanks for your time. :)
Was This Post Helpful? 0
  • +
  • -

#6 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: responseText is empty in IE

Posted 09 June 2008 - 07:20 AM

hello
try it like this
function GetXmlHttpObject(){
 xmlHttp = null;
try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
	try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	catch(e){
		try{xmlHttp=new XMLHttpRequest()
		}
		catch(e){
		Alert("Your Browser Does Not Support AJAX")
		}
	}
}
return xmlHttp;
}


Was This Post Helpful? 0
  • +
  • -

#7 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: responseText is empty in IE

Posted 09 June 2008 - 08:41 AM

No, that still didn't work.

Again, I think it's the way IE is interpreting the Content-Type of the responseText. I really don't think it has to do with the way the GetXmlHttpObject() function is written (I took most of that verbatim from W3C's site).
Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: responseText is empty in IE

Posted 09 June 2008 - 05:17 PM

I am not sure, but this is sounding like IE thinks the permissions are denied.

Add this in one of your js file and see what it pops up when you try it again.

onerror=handleErr;
var txt="";
function handleErr(msg,url,l){
  txt="There was an error on this page.\n\n";
  txt+="Error: " + msg + "\n";
  txt+="URL: " + url + "\n";
  txt+="Line: " + l + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
}


It should tell you what is going on and everything (like Permission Denied, etc.).

If this is the case you may need to change php/getPost.php permission settings to 777 (not sure why that would be needed, normally you are allowed to use ajax on files in your own server (maybe something to do with the way your server is configured?).

Let me know what happens.
Was This Post Helpful? 0
  • +
  • -

#9 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: responseText is empty in IE

Posted 09 June 2008 - 11:22 PM

That didn't produce anything either.

I'm fairly confident that no errors are happening here, it's just IE not sending back the responseText correctly.

I appreciate your help, BetaWar. I hope we can get this figured out soon.
Was This Post Helpful? 0
  • +
  • -

#10 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: responseText is empty in IE

Posted 10 June 2008 - 10:44 AM

Hm, not sure but if I could get you to add a little bit to your php file really quick we may be able to figure out what is going on. At this point when I go here:
http://www.adamdrago...etPost.php?id=1

Nothign is being outputted (in IE), however when looking at your source code it looks like the place that the query is being sent to (assuming you selected the 4-7-08 post option from the dropdown list.

So if I could get you to just add/append your php code (for the http://www.adamdrago...php/getPost.php file) to look something like so I think the problem may become clear (if there is any):

Quote

<?php
$id = $_GET["id"];
require('connect.php');
$theQuery = mysql_query("select post from archive where title='" . $id . "'")or die("There was a problem with the query: " . mysql_error());
$row = mysql_fetch_assoc($theQuery);
echo $row['post'] . "<br><br>Testing output in IE";
?>


With any luck that will output something (even if it is just an error) in IE. At least it should help us narrow down the problem.

<edit>
Okay, nevermind on updating the php I think I found the problem! Yayness!

Okay so your form is not submitting anything in IE, it looks like this currently:

				<select id="post" onchange="changePost(this.value);">
				<option id="10">Accessing a Database with AJAX.</option>
<option id="9">Adding An Archiver.</option>
<option id="8">6-4-08</option>
<option id="7">5-2-08</option>
<option id="6">4-27-08</option>
<option id="5">4-21-08</option>
<option id="4">4-12-08</option>
<option id="3">4-10-08</option>
<option id="2">4-9-08</option>
<option id="1">4-7-08</option>
				</select>


However when you change that to this:

Quote

<select id="post" onchange="alert(this.value);">
<option id="10">Accessing a Database with AJAX.</option>
<option id="9">Adding An Archiver.</option>
<option id="8">6-4-08</option>
<option id="7">5-2-08</option>
<option id="6">4-27-08</option>
<option id="5">4-21-08</option>
<option id="4">4-12-08</option>
<option id="3">4-10-08</option>
<option id="2">4-9-08</option>
<option id="1">4-7-08</option>
</select>


In IE it outputs this: "" (nothing)

So change the above cod eto this and it shoudl work out fine:

Quote

<select id="post" onchange="changePost(this.value);">
<option id="10" value="Accessing a Database with AJAX.">Accessing a Database with AJAX.</option>
<option id="9" value="Adding An Archiver.">Adding An Archiver.</option>
<option id="8" value="6-4-08">6-4-08</option>
<option id="7" value="5-2-08">5-2-08</option>
<option id="6" value="4-27-08">4-27-08</option>
<option id="5" value="4-21-08">4-21-08</option>
<option id="4" value="4-12-08">4-12-08</option>
<option id="3" value="4-10-08">4-10-08</option>
<option id="2" value="4-9-08">4-9-08</option>
<option id="1" value="4-7-08">4-7-08</option>
</select>


I was assuming that you were attempting to post the text to the php form which is the reason that I kept getting no output (there was no article witht he title 1 (or anything else like that) so I tried to input the date of an article and all the sudden it works:

Here is an example query url that works in IE:
http://www.adamdrago...t.php?id=6-4-08

With any luck after you update the form it will all work correctly.
</edit>

This post has been edited by BetaWar: 10 June 2008 - 10:58 AM

Was This Post Helpful? 1
  • +
  • -

#11 atdrago  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 08-June 08

Re: responseText is empty in IE

Posted 10 June 2008 - 08:28 PM

Awesome!

Good sleuthing, BetaWar. It now works in IE. I appreciate all your help, man. Really.

Thanks a bunch!
Was This Post Helpful? 0
  • +
  • -

#12 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: responseText is empty in IE

Posted 11 June 2008 - 10:56 AM

No problem, just glad I was able to help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1