Jump to content

Trouble with AJAX request in new window


Jezevec

Recommended Posts

Hi everyone,

I have this kind of troubles:

 

Using this code:

 

function getXMLHttp()

{

  if(window.XMLHttpRequest)

  {

    return new XMLHttpRequest();

  }

  else if(window.ActiveXObject)

  {

    return new ActiveXObject("Microsoft.XMLHTTP");

  }

  return null;

}

 

function MyFunc(id)

{

  xmlhttp=getXMLHttp();

  if(xmlhttp==null)

  {

    alert("Error creating XMLHttp Request.");

    return;

  }

  xmlhttp.open("GET","file_returning_sent_variable.php",true);

  xmlhttp.send(null);

  xmlhttp.onreadystatechange=function()

  {

    if(xmlhttp.readyState==4)

    {

      alert(xmlhttp.responseText);

    }

  }

}

 

When I call the MyFunc from main window, it works regularly. But when I open a new window using window.open method and within this I call the same function (using the same script in the same external *.js file attached in html head), it doesn't work anyway. Trying alert xmlhttp.status, it's 200 in case of the main window, but 0 in case the newly opened window. So tell me, where's the bug?

 

thanks for answers

Jezevec

 

Link to comment
https://forums.phpfreaks.com/topic/158106-trouble-with-ajax-request-in-new-window/
Share on other sites

And I can answer myself :-)

The bug was not inside the AJAX requesting but in the code of opened window - teh event used to call MyFunc was like this:

 

//...

onclick="MyFunc();location.href=location.href;"

//...

 

Where the location.href is used to refresh the page, but this function don't wait for AJAX response and just refresh the page, which means the response data are lost. I worked around this by replacing the refresh inside the block of if(xmlhttp.readyState==4) condition.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.