|
|
 |
 |
 |
 |
AJAX WOES - Not returning a value from xhttp request
CROSSPOSTED TO comp.lang.javascript 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=working; } http.open("GET", url, true); xmlHttp.send(null); } }
change function ajaxFunction(qs) { //document.getElementById("coords").innerHTML = qs; // check to see that value is being passed - WORKS var xmlHttp; to var xmlHttp; function ajaxFunction(qs) { //document.getElementById("coords").innerHTML = qs; // check to see that value is being passed - WORKS And see if it works. You also should be checking the xmlHttp.status==200after the readystate is 4 Eric On 2/22/07, salvador <spera@progressivetrail.org> wrote:
> CROSSPOSTED TO comp.lang.javascript > 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=working; > } > http.open("GET", url, true); > xmlHttp.send(null); > } > }
Hi Eric, Thanks for the suggestion. I guess you were thinking that xmlHTTP was being assigned as a local variable. That's as good a guess as any, but it didn't work, but I'll keep hacking away at this. On Feb 22, 8:45 am, "Eric Pascarello" <alienf@gmail.com> wrote:
> change > function ajaxFunction(qs) { > //document.getElementById("coords").innerHTML = qs; > // check to see that value is being passed - WORKS > var xmlHttp; > to > var xmlHttp; > function ajaxFunction(qs) { > //document.getElementById("coords").innerHTML = qs; > // check to see that value is being passed - WORKS > And see if it works. You also should be checking the > xmlHttp.status==200after the readystate is 4 > Eric > On 2/22/07, salvador <spera@progressivetrail.org> wrote: > > CROSSPOSTED TO comp.lang.javascript > > 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=working; > > } > > http.open("GET", url, true); > > xmlHttp.send(null); > > } > > }
http://developer.apple.com/internet/webcontent/xmlhttpreq.html Eric On 2/22/07, salvador <spera@progressivetrail.org> wrote:
> Hi Eric, > Thanks for the suggestion. I guess you were thinking that xmlHTTP was > being assigned as a local variable. That's as good a guess as any, > but it didn't work, but I'll keep hacking away at this. > On Feb 22, 8:45 am, "Eric Pascarello" <alienf@gmail.com> wrote: > > change > > function ajaxFunction(qs) { > > //document.getElementById("coords").innerHTML = qs; > > // check to see that value is being passed - WORKS > > var xmlHttp; > > to > > var xmlHttp; > > function ajaxFunction(qs) { > > //document.getElementById("coords").innerHTML = qs; > > // check to see that value is being passed - WORKS > > And see if it works. You also should be checking the > > xmlHttp.status==200after the readystate is 4 > > Eric > > On 2/22/07, salvador <spera@progressivetrail.org> wrote: > > > CROSSPOSTED TO comp.lang.javascript > > > 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=working; > > > } > > > http.open("GET", url, true); > > > xmlHttp.send(null); > > > } > > > }
On Feb 22, 3:11 pm, "salvador" <spera @progressivetrail.org> wrote:
> CROSSPOSTED TO comp.lang.javascript > 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=working; > } > http.open("GET", url, true); > xmlHttp.send(null); > } > }
I would hazard a guess that you're doing cross domain XHR? If you're using a user-supplied URL it could well be out of the domain, and XHR doesn't (naturally) work like that for security reasons. This would also be why it seems to work if you try the URL normally, but the readystate = 4 never works because the XHR is not even trying to send a request off. But I could be wrong... Al
-----------------------------------------------Reply-----------------------------------------------
You should set onreadystatechange after you call open. Also, don't know if this is just a mistake when you posted the code, but when you call open, it is being called on a variable http, which I'm guessing should be xmlHttp "http.open("GET", url, true);" -Jason On Feb 22, 10:11 am, "salvador" <spera@progressivetrail.org> wrote:
> CROSSPOSTED TO comp.lang.javascript > 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=working; > } > http.open("GET", url, true); > xmlHttp.send(null); > } > }
|
 |
 |
 |
 |
|