开发者

passing the global argument to the statechaned function

开发者 https://www.devze.com 2023-03-16 09:24 出处:网络
hi i have a ajax code for deleting a row from db it has a id argument to Identified that row i want to be able to use that after the deleting is done on the stateChangedd function

hi i have a ajax code for deleting a row from db

it has a id argument to Identified that row

i want to be able to use that after the deleting is done on the stateChangedd function

function stateChangedd()
{
  if (xmlhttp.readyState==4)
  {
alert('done');
alert(id);

  }
}

but it says id is undefined

here is the complete code

function deletrow(id)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }



alert(id);

url=encodeURI("handler.php?id="+id+"&do=delete");
xmlhttp.onreadystatechange=stateChangedd;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChangedd()
{
  if (xmlhttp.readyState==4)
  {
alert('done');
alert(id);

  }
}

function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
return objXMLHttp;

} 

i get the first alert which says done! but i cant get the second one

with anonymous function none of the alerts work , it sends the url though

function deletrow(id)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }



alert(id);

url=encodeURI("handler.php?id="+id+"&do=delete");
xmlhttp.onreadystatechange;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

xmlhttp.onreadystatechange=function()

{
  if (xmlhttp.readyState==4)
  {
alert('done');
alert(id); 
 开发者_JS百科 }
}

function GetXmlHttpObject()
{
var objXMLHttp=null;
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
return objXMLHttp;

} 


That's because id is not in stateChangedd's scope. just use the stateChanged function as an anonymous function in onreadystatechange would work:

function deletrow(id)
{

    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }    

    alert(id);

    url=encodeURI("handler.php?id="+id+"&do=delete");
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            alert('done');
            alert(id);
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}


function GetXmlHttpObject()
{
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消