Closed Bug 246518 Opened 20 years ago Closed 8 years ago

XMLHttpRequest open method fails (NS_ERROR_FAILURE) on second call if an instance is used both from a main window and a popup and the first use happens from popup

Categories

(Core :: XML, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: tener, Assigned: hjtoi-bugzilla)

References

()

Details

(Keywords: testcase)

Attachments

(5 files)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608

Page test.htm opens a window with page popup.htm which in turn calls Javascript
function back in test.htm (using opener.xxxx) and then closes itself (the
popup).  The function that is called in test.htm uses XMLHttpRequest.  In the
second call to this function, then open method of the XMLHttpRequest object
fails (error in catch).  My guess is that this problem is related to the fact
that the reference to the XMLHttpRequest object is kept in a global variable and
reused for the second call.  

Note that this problem also occurs in Mozilla 1.6, but in 1.6 the browser crashes.

Reproducible: Always
Steps to Reproduce:
1. Put both test.htm and popup.htm in the same virtual directory -- see attached
pages.
2. Display test.htm in browser
3. Click "Popup Window" button which brings up popup.htm
4. Click "Set Value" button in popup which sets text field in test.htm and
closes popup.
5. Contents of text field are probably HTML for cannot find window at this
point.  Also fails if response is returned.
6. Click "Popup Window" button again which brings up popup.htm
7. Click "Set Value" button in popup which closes opup window and display alert
saying that open method failed.

Actual Results:  
open method of XMLHttpRequest object failed when it should have worked.

Expected Results:  
Make the XMLHttpRequest just like the first tiem "Set Value" was clicked in popup.
See reproduce instructions.
Attached file page for popup window.
See instructions to reproduce.
I really don't know for sure, but isn't this the same issue as bug 229294?
Depends on: 229294
I have made a reduced test case for this bug and will upload the pieces now to
have a test case in bugzilla that can be tested directly by loading one of the
attachments in the bug.
The test case uploaded as
<https://bugzilla.mozilla.org/attachment.cgi?id=173288> showed the error here
for me when run on localhost (all pages coming from localhost) but somehow now
when loaded from https://bugzilla.mozilla.org/ no problem occurs. I will try to
find out what could be the difference (https versus http?) between running the
test case on bugzilla and localhost but for now I leave the bug as unconfirmed.
I have put my test case on a public HTTP server at
<http://home.arcor.de/martin.honnen/mozillaBugs/XMLHttpRequest/window-and-popup/mainTest1.html>
and there indeed the problems occurs and is reproducible so I am confirming this
bug.
The test case requires user interaction as a popup needs to be opened by script
and when it is closed a further button on the page needs to be clicked.
To reproduce the problem load the test case in a Mozilla window, once it is
loaded press the button with label "test popup", wait till a popup appears and
disappears.
The popup successfully uses the XMLHttpRequest object instantiated in the main
window to do a HTTP GET request for another HTML page.
Then press the button below with label "test" and check the JavaScript console,
it shows

Error: uncaught exception: [Exception... "Component returned failure code:
0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.open]"  nsresult: "0x80004005
(NS_ERROR_FAILURE)"  location: "JS frame ::
http://home.arcor.de/martin.honnen/mozillaBugs/XMLHttpRequest/window-and-popup/mainTest1.html
:: doTest :: line 35"  data: no]

when trying to use the XMLHttpRequest object instantiated earlier to again to a
HTTP GET request for an HTML page. Any further clicks on both buttons then
produce the same exception, unless the main page is reloaded.

Doing the same stuff in IE 6 works flawlessly while the test case when used as
described fails with several Mozilla versions:
- recent nightly Mozilla 1.8b (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8b) Gecko/20050130)
- Firefox 1.0 release (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)
Gecko/20041107 Firefox/1.0)
- Netscape 7.2 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2)
Gecko/20040804 Netscape/7.2 (ax))

Note that Mozilla suite versions but not Firefox versions throw an additional
exeception

Error: uncaught exception: [Exception... "Component returned failure code:
0x80004005 (NS_ERROR_FAILURE) [nsIControllers.removeController]"  nsresult:
"0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame ::
chrome://navigator/content/navigator.js :: Shutdown :: line 751"  data: no]

in the JavaScript console already when the popup closes itself, I am not sure
whether this is somehow related or at least helps to find the cause of the problem.

Note also that the test case works with Mozilla if you load it in a window and
then first press the second button and only then the first button to open the
popup, then both the main window and the popup window can make as many HTTP
requests as wanted with the same instance of XMLHttpRequest without throwing the
exception on the open method call.

The mentioned exeception when the popup is closed however occurs every time.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Adjusting summary to reflect the findings with my test case.

The same exception also occurs for someone in a production environment making
heavy use of XMLHttpRequest, see the Usenet thread
<http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/b221d377dc3bfde2/85aedf6b3927279d?q=group:comp.lang.javascript+insubject:XMLHttpRequest&_done=%2Fgroups%3Fsafe%3Dimages%26as_ugroup%3Dcomp.lang.javascript%26as_usubject%3DXMLHttpRequest%26as_scoring%3Dd%26lr%3D%26hl%3Den%26&_doneTitle=Back+to+Search&&d#85aedf6b3927279d>
although there no popup windows are used but an XMLHttpRequest object instance
is shared by several frames as far as I understand. A workaround there was to
create a new instance of XMLHttpRequest when needed but that is of course only a
workaround.
Summary: XMLHttpRequest open method fails on second call from popup window → XMLHttpRequest open method fails (NS_ERROR_FAILURE) on second call if an instance is used both from a main window and a popup and the first use happens from popup
Keywords: testcase
So the reason this fails is that you're using a relative URI.  The
XMLHttpRequest object gets its base URI from the document associated to the
script context it's holding on to (which it caches).

Now in this case, the script context is that of the closed window.  Hence it has
no document, and there is no base URI.  So getting a resolved URI to fetch fails.

jst, should we be clearing the current context in here somewhere?  I do think
it's wrong that mCurrentContext is the closed window if open() is called from
the remaining window...
(In reply to comment #10)
> So the reason this fails is that you're using a relative URI.  The
> XMLHttpRequest object gets its base URI from the document associated to the
> script context it's holding on to (which it caches).
> 
> Now in this case, the script context is that of the closed window.  Hence it has
> no document, and there is no base URI.  

If it is the caching of the script context that is causing this then indeed this
bug and https://bugzilla.mozilla.org/show_bug.cgi?id=229294 have the same cause
although the problems caused (exception here, wrong file fetched there) are
different.
I had this identical problem with FF-1.5.0.1 on both linux and WinXP.  I confirmed what Martin said about adding the full path for the URL that the XmlHttpRequest object is pointed to solve the problem.  I.e., instead of oXmlHttpRequest.open("POST", "/rootfolder/script.php", true);, I started using oXmlHttpRequest.open("POST", "http://servername/rootfolder/script.php", true);, and the error went away.  Kind of a wierd problem since both the main page and popup page I'm using are on the same server, but it works.  Thanks for pointing that out - I would have been in real trouble otherwise.
I have the same problem with all known versions of Firefox. Using a relative or full path doesn't work for me neither on a server nor locally.

Testcase:
http://www.birgin.org/test/xmlhttprequest_popup.html
(Click on "Popup", in the popup check "Autoclose" and click on "Load file")

Only workaround I've found, is to wait for a response and close the popup afterwards.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20060718 Minefield/3.0a1
Here are some additional diagnostics, fyi. I'm getting a similar error message from a XUL window. A simple code fragment I'm using to test is attached.

1.
Normally, the code needs to execute just after receiving at event/message (from the observer service). If I call testSendXmlHttpRequest() directly, it fails. However, if I come out of the code that is processing the event, and then execute testSendXmlHttpRequest(), it works. The second alternative is acheived by replacing the call testSendXmlHttpRequest() with window.setTimeout("testSendXmlHttpRequest()", 1000); 

This is unfortunately a lousy way to get the job done. 

2.
Here is by test function.

function testSendXmlHttpRequest()
{
  try
  {
    var url = "locahost/xyz.htm"; // replace with your test url
    var request = new XMLHttpRequest();
    request.onload = function(event) 
    {
       try 
       {
          var text = this.responseText;
          if(text)
             dump("text=" + text + "\n");
          else
             dump("empty responseText\n");
				
         // TODO add handler for text
       } catch(err) {
            dump(err);
            return;
       }
     }

     request.onerror = function(event)
     {
        dump("Error loading page\n");
        try 
        {
           dump("status: " + this.status);				
        }
        catch (err) {};
        return;
     }
		
     request.open("GET", url, true);
     request.send(null);
     return true;

   } catch (err) {
       dump(err);
   }
}



QA Contact: ashshbhatt → xml
I'm guessing this is no longer an issue, as the test-case in the URL field seems to work fine for me on today's nightly, no matter how many times I alternate between clicking "test popup" and "test" (or in a random order). Shall we close this ticket?
Flags: needinfo?(bugs)
Feel free to resolve as worksforme. If there are still issues, one can always reopen the bug or file a new one.

Thanks for going through these old bug!
Flags: needinfo?(bugs)
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: