SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy ajax not working in IE, only working in Firefox...

    hi, i'm a beginner of ajax...

    i've tried on some function on ajax and tested in the server,
    then i found that ajax is not working in IE, only working in Firefox...

    why???
    HTML Code:
    <script language="JavaScript" src="ajax.js"></script>
    
    <script language="JavaScript">
    function doSomethingWithData(str)
    {
    	document.getElementById('menu1').innerHTML = str;
    }
    
    function ajaxFunction(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('menu1');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	var menu= document.getElementById('menu').value;
    	var queryString = "?menu=" + menu;
    	ajaxRequest.open("GET", "item.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    
    </script>
    i did add these to the <head></head> but it still not working..
    HTML Code:
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="EXPIRES" CONTENT="-1">
    is there any other solution to try on?

    thank you!

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add this in your IE try block:

    try{
    new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }

    IE7 will properly handle the new XMLHttpRequest, but various versions of IE6 have different ActiveX objects.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo,

    thanks for your reply...

    i've tried on your suggestion... however it is not working for me...
    mmm~ i guess i must have done something wrong in my code.
    i'm now debugging it...

    wish me luck!

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm... i double checked...

    the code seems fine.

    is there any other way to solve this problem in IE?

    my program is actually enable user to select their top movie genre from a list,when item is selected,it will save the selection into database and then display the item icon on the page.

    everything works fine except in IE, it failed display the selected icon on the page & update the data in database too...

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES!

    i got it solved finally!!!

    the trick is here:
    var menu= document.getElementById('menu').value;
    var queryString = "?menu=" + menu+"anything=1212121212";
    ajaxRequest.open("GET", "item.php" + queryString, true);
    ajaxRequest.send(null);


    just add an extra variable behind this line:
    var queryString = "?menu=" + menu+"anything=1212121212";

    but just wondering why should we do this to make it works...?

  6. #6
    SitePoint Zealot bensheard's Avatar
    Join Date
    Jun 2006
    Location
    Cape Town | South Africa
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kaylie View Post
    but just wondering why should we do this to make it works...?
    Hi

    I have just been reading the trial sample of the book Build Your Own Ajax Based Web Applications and have come across the reason for the above quote:

    Notice that we’ve added to the target URL a query string that has the
    start

    value as a parameter. We’re not actually going to use this value on the server;

    we’re just using it as a random value to deal with Internet Explorer’s overzealous
    caching. IE caches all GET requests made with XMLHttpRequest, and one way of
    disabling that “feature” is to append a random value into a query string. The
    milliseconds value in start can double as that random value. An alternative to
    this approach is to use the setRequestHeader method of the XMLHttpRequest

    class to set the
    If-Modified-Since header on the request.
    Finally, we kick everything off by attaching doPoll to the window.onload event.
    Ben Sheard
    -------------
    Log all your dives easily and quickly from anywhere in the world
    www.divinglogs.cafe150.com

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bensheard,

    oh... that's the reason...

    now i know already...thanks for sharing!!!

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we’re just using it as a random value to deal with Internet Explorer’s overzealous
    caching. IE caches all GET requests made with XMLHttpRequest
    now i know already...thanks for sharing!!!
    Apparently, you suspected it was a cache problem based on this attempt to correct the problem:
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="EXPIRES" CONTENT="-1">
    The reason this works:

    var queryString = "?menu=" + menu+"anything=1212121212";

    is because the browser previously cached a page with the the following url:

    somePage.com?menu=aMenu

    (which means the browser saved the page associated with that url, so that the browser doesn't have to go back to the server to get the page again, which saves time)

    However, the browser does not consider the following urls to be equivalent:

    somePage.com?menu=aMenu
    somePage.com?menu=aMenu&anything=123434

    so when the browser sees the second url, it checks its cache and it doesn't find that url there, so the browser requests the page from the server.

    Note that your trick will only work one time because then the page associated with the url:

    somePage.com?menu=aMenu&anything=123434

    will be cached, so the next time the browser sees a request for that page, it will display the page in the cache rather than getting it from the server. That is why sometimes people will call random() and get a random number and then tack the random number onto the end of the url--that makes the browser request the page from the server everytime because the url will always be different from any previous urls.


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
  •