Quote:
Originally Posted by devnull69
You stumbled upon the most common pitfall in using Ajax
The first "A" in Ajax stands for "asynchronous". That means: The javascript code following the Ajax request .send() continues to execute while(!) the request is still running. A process in the background will call the onreadystatechange callback as soon as the readyState of the request changes. As soon as the readyState reaches 4 the request is finished.
That results in two facts:
1. If you want to alert the result of the request outside of the callback it will not work
2. Everything you want to do with the result of the request has to be done in (or started from) the onreadystatechange callback
So if you move the code from below the .send() into the onreadystatechange callback, it will start to work all of a sudden
|
I followed what you have advised earlier. But failed to get result it was returning true each time. Then i used this code. Anyways, again i tried what you have advised and find to get correct result. Posting modified code below.
function contract(count_number)
{
var item1=document.getElementById('ItemId1'+count_number).value;
var response=" ";
if(window.XMLHttpRequest)
{
contract_xml=new XMLHttpRequest();
}
else
{
contract_xml=new ActiveXObject("Microsoft.XMLHTTP");
}
contract_xml.onreadystatechange=function()
{
if(contract_xml.readyState==4)
{
//alert("4");
response=contract_xml.responseText;
if(response==1)
{
if(confirm('Please confirm that this is a binding contract '))
{
return true;
}
else
{
return false;
}
}
else
{
alert("Sorry, you are a little late. ");
return false;
}
}
// else if( (contract_xml.readyState==3) || (contract_xml.readyState==2) || (contract_xml.readyState==1) )
// {
// response="";
// }
};
contract_xml.open("GET","<?php echo $site_url; ?>check_available_offer.php?item_id="+item1,true);
contract_xml.send();
}
But is there any other solution? Can you just modify this?