By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,332 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,332 IT Pros & Developers. It's quick & easy.

AJAX Woes - XML HTTP Request not returning a value

P: n/a
I'm trying to create a function to return some values from a php
script. The php script is returning the correct values if called from
a browser window. However, the function that I'm using never appears
to pass the condition 'if(xmlHttp.readyState==4)'.

Can anyone see where I'm going wrong, or alternately, offer better
(read:working) solution?

function ajaxFunction(qs) {
//document.getElementById("coords").innerHTML = qs;
// check to see that value is being passed - WORKS
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
url = url + qs;
//document.getElementById("coords").innerHTML= url;
// verify that URL is property constructed - WORKS
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
var working=xmlHttp.responseText;
// verify that a value is getting returned as text - FAILS
document.getElementById("coords").innerHTML=workin g;
}
http.open("GET", url, true);
xmlHttp.send(null);
}
}

Feb 22 '07 #1
Share this Question
Share on Google+
5 Replies

P: n/a
salvador wrote:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
var working=xmlHttp.responseText;
// verify that a value is getting returned as text - FAILS
document.getElementById("coords").innerHTML=workin g;
}
http.open("GET", url, true);
xmlHttp.send(null);
}
}
Move the open and send call out of the onreadystatechange handler

if(xmlHttp.readyState==4)
{
var working=xmlHttp.responseText;
// verify that a value is getting returned as text - FAILS
document.getElementById("coords").innerHTML=workin g;
}
http.open("GET", url, true);
xmlHttp.send(null);

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 22 '07 #2

P: n/a
On Feb 22, 7:13 am, Martin Honnen <mahotr...@yahoo.dewrote:
salvador wrote:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
var working=xmlHttp.responseText;
// verify that a value is getting returned as text - FAILS
document.getElementById("coords").innerHTML=workin g;
}
http.open("GET", url, true);
xmlHttp.send(null);
}
}

Move the open and send call out of the onreadystatechange handler

if(xmlHttp.readyState==4)
{
var working=xmlHttp.responseText;
// verify that a value is getting returned as text - FAILS
document.getElementById("coords").innerHTML=workin g;
}
http.open("GET", url, true);
xmlHttp.send(null);

--

Martin Honnen
http://JavaScript.FAQTs.com/

Hey Martin,

Thanks for the suggestion. Apparently, not what the problem was, but
I'll keep poking at it.

-Sal

Feb 22 '07 #3

P: n/a
VK
On Feb 22, 6:03 pm, "salvador" <spera...@progressivetrail.orgwrote:
I'm trying to create a function to return some values from a php
script. The php script is returning the correct values if called from
a browser window. However, the function that I'm using never appears
to pass the condition 'if(xmlHttp.readyState==4)'.

Can anyone see where I'm going wrong,
As Martin Honnen pointed out, you script is never executed: remove
open and send out of onreadystatechange handler and place them in the
same block where you create XHR.
or alternately, offer better (read:working) solution?
AjaxRequest by Matt Kruse <http://www.ajaxtoolbox.com>

Feb 22 '07 #4

P: n/a
On Feb 22, 12:44 pm, "VK" <schools_r...@yahoo.comwrote:
On Feb 22, 6:03 pm, "salvador" <spera...@progressivetrail.orgwrote:
I'm trying to create a function to return some values from a php
script. The php script is returning the correct values if called from
a browser window. However, the function that I'm using never appears
to pass the condition 'if(xmlHttp.readyState==4)'.
Can anyone see where I'm going wrong,

As Martin Honnen pointed out, you script is never executed: remove
open and send out of onreadystatechange handler and place them in the
same block where you create XHR.
Yes. That was a problem with the script. However, it was not the
primary problem that I am encountering. Even using the library that
you recommended below, I cannot get an XHR request to evaluate true.

See for yourself:

http://oregonhomefinder.net/test/
>
or alternately, offer better (read:working) solution?

AjaxRequest by Matt Kruse <http://www.ajaxtoolbox.com>

Feb 22 '07 #5

P: n/a
On Feb 22, 2:35 pm, "salvador" <spera...@progressivetrail.orgwrote:
On Feb 22, 12:44 pm, "VK" <schools_r...@yahoo.comwrote:
On Feb 22, 6:03 pm, "salvador" <spera...@progressivetrail.orgwrote:
I'm trying to create a function to return some values from a php
script. The php script is returning the correct values if called from
a browser window. However, the function that I'm using never appears
to pass the condition 'if(xmlHttp.readyState==4)'.
Can anyone see where I'm going wrong,
As Martin Honnen pointed out, you script is never executed: remove
open and send out of onreadystatechange handler and place them in the
same block where you create XHR.

Yes. That was a problem with the script. However, it was not the
primary problem that I am encountering. Even using the library that
you recommended below, I cannot get an XHR request to evaluate true.

See for yourself:

http://oregonhomefinder.net/test/
or alternately, offer better (read:working) solution?
AjaxRequest by Matt Kruse <http://www.ajaxtoolbox.com>
The problem appears to be that the result never gets to a requestReady
state of 4. The transaction appears to be working but always stops at
3.

Why would this be?

Feb 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.