Forum Moderators: open

Message Too Old, No Replies

xmlhttprequest problem

         

JevgeniBogatyrjov

7:52 am on Aug 9, 2010 (gmt 0)

10+ Year Member



My problem is that I want to return a boolean value using xmlhttprequest, but it only works when i stick the responsetext to a DOM object. The "return xmlhttp.responseText;" returnes "undefined".

Can I somehow make a request return a value without writing it anywhere?

function sendGet(url){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
document.getElementById('foo').innerHTML = xmlhttp.responseText;
//WORKS FINE
return xmlhttp.responseText; //RETURNS "UNDEFINED"
}else{
alert('There was a problem with the request.');
}
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

JevgeniBogatyrjov

8:23 am on Aug 9, 2010 (gmt 0)

10+ Year Member



ok, I understand, my code returns a value of "on the fly" function, not the whole function, but still, I don't get hot to make this work

Fotiman

1:18 pm on Aug 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A return value to where? You're doing an asynchronous request to the server, which means the page doesn't have to wait for a response from the server before moving on, so when your onreadystatechange method is called you're not in any context where a return value makes sense. You could make it a synchronous request, but there is a big difference between synchronous or asynchronous, and you should probably get a better understanding of the two models before relying on synchronous requests.

JevgeniBogatyrjov

1:39 am on Aug 14, 2010 (gmt 0)

10+ Year Member



I'll try to explain this problem of mine:

- I must retreive some info from php script while running a javascript application.
- The aim of function sendGet(url) is to get that info from a PHP script.
- Function sendGet(url) writes the answer of the php script to document.getElementById('foo').innerHTML on onreadystatechange. Perfect.
- But I don't need this info from the server to be written in a DOM object, I must use it in my JS code. The question is - how can I do that?
- Also, I am not allowed to use PHP scripting in my javascript application, I must retreive info from PHP scripts only using xmlhttprequest.

Fotiman

1:40 pm on Aug 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




- But I don't need this info from the server to be written in a DOM object, I must use it in my JS code. The question is - how can I do that?

What is it you need to do with the value? You could assign it to a global variable, but again, this is executing asynchronously so there's no way to know when that value is getting set unless you have some other function that you're calling. So what exactly is it that you need to do with this value?

JevgeniBogatyrjov

12:12 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



I need to check whether the user is logged in or not when he presses a button. The login state defines further behavior.

In my PHP script, I have a variable, which shows whether the user is logged in and which my JS needs to get when someone presses the button.

Thanks for answering =)

Fotiman

1:24 pm on Aug 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you trying to do something like this?

//onclick="foo();"
function foo() {
if (sendGet('SOMEURL')) {
// User is logged in... do stuff
loggedin();
}
else {
// User is not logged in...
notloggedin();
}
}

If that's the case, you need to change your code to have your onreadystatechange function call loggedin or notloggedin.


xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
if (xmlhttp.responseText) {
loggedin();
}
else {
notloggedin();
}
}else{
alert('There was a problem with the request.');
}
}
}


And then your foo function would just be:


//onclick="foo();"
function foo() {
sendGet('SOMEURL');
}

JevgeniBogatyrjov

10:16 pm on Aug 18, 2010 (gmt 0)

10+ Year Member



Worked fine! Many many thanks to you!