Forum Moderators: open
I have an AJAX function that simply calls a PHP file and receives a value from it. Here's the code:
var result = null;httpobj = new XMLHttpRequest();
httpobj.open('get', 'file.php', true);
httpobj.send(null);
httpobj.onreadystatechange = function()
{
if (httpobj.readyState == 4 && httpobj.status == 200)
{
self.result = httpobj.responseText;
}
}
alert(self.result);
I declared a global variable called result that is visible throughout my javascript file. I then attempted to store the httpobj.responsetext to this variable but then after I did an alert after the function call, I received null.
After thinking about it, it makes sense because on onreadystatechange I'm calling a function that appear to have a "hidden" scope where global variables are not recognized, if that makes sense.
Also, any subsequent functions I call from within function() are also part of this "hidden" scope.
Does anyone know how I can store the value of responsetext to a variable that I've declared globally, in this case, result?
I do not want to store this into any HTML tags and retrieve it again but rather access the value directly from one variable to another.
Any ideas and help would be helpful and appreciated!
Bobby Pejman
I'm not sure what other code might be at play here, but changing "self.result" to "this.result" and then alerting "httpobj.result" may work for you.
The alert will fire before your httpobj has received a response though, so you'll want to move the alert into the onreadystatechange function.
Thanks for the reply! I did what you mentioned and still no luck. As an FYI, I purposely put the alert outside of the onreadystate function because I'd like to capture the result of responseText and store it within a global variable in the same javascript file.
The issue here is that I'm working with different scopes where one has no visibility to the other. Anything that falls into the function(){} scope vanishes once the function is ended. It also doesn't see any global variables once I enter the function scope. The sad news I'm hearing is that what I'm trying to do is impossible. At least, after countless hours of Googling.
Thanks for the reply again, though. If anyone has managed to solve this, it would really help me out!
Add an alert('testing') inside of the callback function to ensure that it is even executing.
Remove the "self." (or "this.") prefix from the result variable. If result is declared within the same <script> tag (or an earlier one) in the head of the document, and outside of any function, then it will be visible to the callback function.
If file.php has any logic in it right now, then replace it with a stripped down version that just returns a single value for testing purposes.
If the original code you posted has lines removed, then post the entire block.
Here's my new JavaScript code that captures and stores the value of responseText. Hopefully this will help others.
var interval;
var result = null; // Set this accordingly based on various return values from your PHP file
httpobj = new XMLHttpRequest(); //FF for now
httpobj.open('get', 'file.php', true);
httpobj.send(null);
httpobj.onreadystatechange = function()
{
if (httpobj.readyState == 4 && httpobj.status == 200)
{
result = httpobj.responseText;
}
}
interval = setInterval("getResult()", 1000);
// Alert the value of result and clear interval
function getResult()
{
// once we get a result, turn interval off.
if(result != null)
{
interval = clearInterval(interval);
alert(result); // we're clearly out of the onreadystatechange scope with our result.
}
}
Ok, I reviewed your code and to answer your question, the setInterval() is a built-in JS function that executes at a specified millisecond interval. In this case, I have it set to check on the onreadystatechange so as soon as a result is returned by AJAX. It will check every second (1000 milliseconds) until a result is returned. I would recomend you reduce this number to 100. The result from Ajax is returned a lot faster so your program/function doesn't have to waste an entire second waiting.
Keep in mind that if you reduce it to a low number, it will cause the setInterval to execute at a much faster rate so consider that.
Also, you might want to take a look at setInterval(), setTimeout(), clearInterval() and so on because they will come in handy.
Now, I think what your problem is that you want to put "&& xmlhttp.readyState == 200" in your IF statement where readyState == 4. Simply having a readystate of 4 is not enough.
Also, take a look and make sure that the parameter you're passing in to your PHP file ("+hyperLink[2]) is being picked up. I don't know what's going on in your PHP file so I can't troubleshoot but if you're doing a $_Get['q'], you might want to make sure it's working.
Try that and let me know how it goes.