Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Beginners AJAX Problem

    Hi Im new to ajax and I am having a problem with this script

    ajax.js:
    Code:
    function createRequestObject() {
        var connect;
        var browser = navigator.appName;
        if(browser == 'Microsoft Internet Explorer'){
            connect = new ActiveXObject('Microsoft.XMLHTTP');
        }
        else{
            connect = new XMLHttpRequest();
        }
        return connect;
    }
    
    var http = createRequestObject();
    
    function ajax(url, id) {
         load.style.display = 'block';
    	 http.open("GET", url, true);
         http.onreadystatechange = handleResponse(id);
    	 http.send(null);
    }
    
    function handleResponse(id) {
        if(http.readyState == 4 && http.status == 200){
              document.getElementById(id).innerHTML = http.responseText;
              load.style.display = 'none';
        }
        else{
          alert("AJAX Error. Please Reload The Page");
        }
    }

    test.html:
    Code:
    <script language="JavaScript" src="ajax.js" type="text/javascript"></script>
    <style type="text/css">
    #load {
    	position:absolute;
    	left:0;
    	top:0;
    	width:70px;
    	height:20px;
    	background-color: #CC4444;
    	color: white;
        display: none;
    }
    </style>
    
    <div id="load">Loading...</div>
    
    <a href="javascript:ajax('test.txt', 'content')">Load Something</a>
    
    <br />
    <br />
    
    <div id="content">
    </div>
    I get that alert message at the bottom when I try to run it.

    Any ideas on how to fix it?

  • #2
    eak
    eak is offline
    Regular Coder eak's Avatar
    Join Date
    Jun 2002
    Location
    Nashville, TN
    Posts
    354
    Thanks
    0
    Thanked 26 Times in 26 Posts
    when you set http.onreadystatechange it should not be a function call.
    for example
    http.onreadystatechange = handleResponse(id);
    should be
    http.onreadystatechange = handleResponse;
    or
    http.onreadystatechange = function(){handleResponse(id);}
    eak | "Doing a good deed is like wetting your pants; every one can see the results, but only you can feel the warmth."

  • #3
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It worked. Thanks!!!!

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Your code is calling the wrong processing for most versions of Internet Explorer (and browsers masquerading as Internet Explorer).

    IE7 (and all browsers except IE) should be using XMLHttpRequest();
    IE6 can most likely use "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", or at worst "MSXML2.XmlHttp.3.0".
    Earlier versions of IE may also be able to use one of these (depending on what patches have been installed) and only if none of these are available should "MSXML2.XmlHttp" or Microsoft.XmlHttp" be used.

    See http://javascript.about.com/library/blajax01.htm for a way to code the request so that it gets the best possible connection in each case.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This may be a little off topic, but I just wanted to let EAK know that his signature about doing good deeds is frickin' awesome.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #6
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall
    Your code is calling the wrong processing for most versions of Internet Explorer (and browsers masquerading as Internet Explorer).

    IE7 (and all browsers except IE) should be using XMLHttpRequest();
    IE6 can most likely use "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", or at worst "MSXML2.XmlHttp.3.0".
    Earlier versions of IE may also be able to use one of these (depending on what patches have been installed) and only if none of these are available should "MSXML2.XmlHttp" or Microsoft.XmlHttp" be used.

    See http://javascript.about.com/library/blajax01.htm for a way to code the request so that it gets the best possible connection in each case.
    Thank you for that sir... thank you very much!
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.


  •  

    Posting Permissions

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