SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't get simple XMLHttpRequest to work in FF1.0.7

    The following script works fine in IE6. In FF 1.0.7, it doesn't display the response text. The alert()'s in the script show that:

    1) the script successfully creates the request object
    2) 'http_request.status' starts off equal to 0
    3) the readystate displays: 1, 1, 2, 4
    4) an error occurs when the script tries to access the .status property of the request object after the request has been sent and the readyState reaches 4(line marked in red below).
    Code:
    <html>
    <head><title></title>
    
    <script type="text/javascript" language="javascript">
    
    function makeRequest(url) 
    {
    	var http_request = false;
    	
    	if (window.XMLHttpRequest)
    	{ // Mozilla, Safari,...
    		
    		http_request = new XMLHttpRequest();
    		alert(http_request); //object XMLHttpRequest
    		alert("status: " + http_request.status); //status: 0
    		/*
    		alert("overriding mime type");
    		http_request.overrideMimeType('text/xml');
    		*/
    		
    	}
    	else if (window.ActiveXObject) 
    	{ // IE
    		try 
    		{
    			http_request = new ActiveXObject("Msxml2.XMLHTTP");
    		} 
    		catch (e) 
    		{
    			try 
    			{
    				http_request = new ActiveXObject("Microsoft.XMLHTTP");
    			} 
    			catch (e) {}
    		}
    	}
    
    	if (!http_request) 
    	{
    		alert('Giving up :( Cannot create an XMLHTTP instance');
    		return false;
    	}
    	
    	http_request.onreadystatechange = function() { alertContents(http_request); };
    	http_request.open('GET', url, true);
    	http_request.send(null);
    }
    
    function alertContents(http_request)
    {
    	alert(http_request.readyState);
    	if (http_request.readyState == 4) 
    	{
    		
    ERROR --->      if(http_request.status) alert("status property exists");
                    else alert("no status property");
    		
    		if (http_request.status == "200") 
    		{
    			alert(http_request.responseText);
    		}
    		else
    		{
    			alert('There was a problem with the request.');
    		}
             } 
    }
    
    
    </script>
    </head>
    <body>
    
    <div><a href="" onclick="makeRequest('someText.txt')">click me</a></div>
    
    </body>
    </html>
    Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost:8080/programs/XMLHttpRequest2.htm :: alertContents :: line 55" data: no]
    Source File: http://localhost:8080/programs/XMLHttpRequest2.htm
    Line: 55

    someText.txt:
    Code:
    response text: hello
    Last edited by 7stud; Mar 11, 2006 at 00:20.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about FF1.5?

    Do you get the error if you make "http_request" a global variable instead of passing it as an argument to the event handler?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for taking a look.

    What about FF1.5?
    I haven't tested it in FF1.5.

    Do you get the error if you make "http_request" a global variable instead of passing it as an argument to the event handler?
    Yes. Also, notice that in the first line of the event handler function I have an alert() that appears to display the readyState property of the object correctly as it updates, so that is an indication that the object gets passed correctly.

    The script is even from a mozilla tutorial:

    http://developer.mozilla.org/en/docs...etting_Started

    I can't figure out how the status property can exist when the request object is created, but then trying to access the status property later gives an error. I can't even do this check:
    Code:
    if (http_request.readyState == 4) 
    	{
    		if(http_request) 
    		{
    			alert("http_request exists"); //this alert()'s
    			
    			if(http_request.status) //error, nothing displays
    			{
    				alert("http_request status exists");
    			}
    			else
    			{
    				alert("no status property");
    			}
    		}
    Even mentioning http_request.status produces an error. If you do this:
    Code:
    var myObj = {};
    if(myObj.status)alert("yes");
    else alert("no");
    you won't get an error--'no' will display.
    Last edited by 7stud; Mar 11, 2006 at 04:35.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $#!$#@$@#$@#$@#!$@#!$@#!%%#$@#!@#!$!@#$@#!$@#!$@#!$@#
    %#$#$#@%%@$@#$%@#$!@!!!!!!!!!

    Code:
    <a href="" onclick="makeRequest('someText.txt')">click me</a>
    It works in FF if I add:
    Code:
    <a href="" onclick="makeRequest('someText.txt');return false">click me</a>
    So because of the asynchronous nature of the request, the onclick event continued on its merry way and before the request could finish, the page unloaded, yet subsequently some js was still able to execute and and check the status of a non-existent request object, which produced an error on the same line every time?

  5. #5
    SitePoint Zealot execute's Avatar
    Join Date
    Dec 2005
    Location
    Florida
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are you checking for status near the XMLHTTPRequest function, the .status is only there to check the server-code of your serverside file... NOT the XML function.

    Also do not even check for http.status... you don't need it. Just remove that whole line. See if the errors go away
    Inferno Programming Tutorials has articles by experts.

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's odd. At least we know what's causing the problem now. Why do you have an href attribute with an empty value anyway?

    execute, the status property is the HTTP Response code. "200" means a successful response. It's the same as with normal web pages and other files. Checking the status is a good thing.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you have an href attribute with an empty value anyway?
    It's how my text editor is setup to automatically produce <a> tags.

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What editor are you using?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EditPlus.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •