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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.