#1
  1. Almighty God of Nothing
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2005
    Location
    Rotherham, UK
    Posts
    712
    Rep Power
    95

    AJAX not passing POST vars in IE7/IE8


    0 down vote favorite


    Please Note: answers along the lines of 'use jquery' or 'use (insert wellknown framework)' is not helpful. Frameworks such as jquery includes alot of extra code which is not nessary at all for what I am doing. 'But, you can include one from Google', yes that may be the case, but I prefer to keep to my own code. With that in mind, lets proceed to the problem....

    I have an ajax call which doesn't pass POST vars through on IE7/IE8, but only on odd occasions. It appears to be extremely random and the majority of the time it does work.

    I am had a look at jquery and cannot see much difference in the way it works compared to this custom one.

    Here is the ajax function:

    Code:
    function GetXmlHttpObject(handler){ 
      var objxml = null;
     if(handler==null) {
      handler = function() {}
     }
    
       var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];              
    
         try {  
             objxml = new XMLHttpRequest();  
         }  
         catch(e) {  
             for (var i = 0; i < ProgID.length; i++){  
                 try {  
                     objxml = new ActiveXObject(ProgID[i]);  
                 }  
                 catch(e) {  
                     continue;  
                }  
             }  
         } 
     objxml.onreadystatechange=handler;
    
         return objxml;  
    }


    A function that calls the Ajax function would be like this:

    Code:
    function sample_ajax(object_type,object_id) {
     var d = new Date();
     var time = d.getTime();
     var url= MYSITEURL + "my_ajax_script.php?timestamp="+time;
    
     params = "object_type="+object_type+"&object_id="+object_id;
     xmlHttp_comment_notifyreset = GetXmlHttpObject(sample_ajax_helper);//fails on safari 1
     xmlHttp_comment_notifyreset.open("POST", url , true);
    
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-cache");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-store");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "must-revalidate");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "post-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "pre-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    
    
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Length", params.length);
     xmlHttp_comment_notifyreset.setRequestHeader("Connection", "close");
    
     xmlHttp_comment_notifyreset.send(params);
    }
  2. #2
  3. No Profile Picture
    http://stealthwd.ca
    Devshed Novice (500 - 999 posts)

    Join Date
    Dec 2005
    Posts
    706
    Rep Power
    203
    Originally Posted by davidb2002
    0 down vote favorite


    Please Note: answers along the lines of 'use jquery' or 'use (insert wellknown framework)' is not helpful. Frameworks such as jquery includes alot of extra code which is not nessary at all for what I am doing. 'But, you can include one from Google', yes that may be the case, but I prefer to keep to my own code. With that in mind, lets proceed to the problem....

    I have an ajax call which doesn't pass POST vars through on IE7/IE8, but only on odd occasions. It appears to be extremely random and the majority of the time it does work.

    I am had a look at jquery and cannot see much difference in the way it works compared to this custom one.

    Here is the ajax function:

    Code:
    function GetXmlHttpObject(handler){ 
      var objxml = null;
     if(handler==null) {
      handler = function() {}
     }
    
       var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];              
    
         try {  
             objxml = new XMLHttpRequest();  
         }  
         catch(e) {  
             for (var i = 0; i < ProgID.length; i++){  
                 try {  
                     objxml = new ActiveXObject(ProgID[i]);  
                 }  
                 catch(e) {  
                     continue;  
                }  
             }  
         } 
     objxml.onreadystatechange=handler;
    
         return objxml;  
    }


    A function that calls the Ajax function would be like this:

    Code:
    function sample_ajax(object_type,object_id) {
     var d = new Date();
     var time = d.getTime();
     var url= MYSITEURL + "my_ajax_script.php?timestamp="+time;
    
     params = "object_type="+object_type+"&object_id="+object_id;
     xmlHttp_comment_notifyreset = GetXmlHttpObject(sample_ajax_helper);//fails on safari 1
     xmlHttp_comment_notifyreset.open("POST", url , true);
    
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-cache");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-store");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "must-revalidate");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "post-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "pre-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    
    
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Length", params.length);
     xmlHttp_comment_notifyreset.setRequestHeader("Connection", "close");
    
     xmlHttp_comment_notifyreset.send(params);
    }
    Yo. Everything looks ok to me.... I've never used the cache control stuff with ajax, so I can't tell you if there are problems there. Have you tried putting a try \ catch around the guts of your sample_ajax function? If so, can you tell me what it catches?

    And, on a side note, I'm with you, I usually write everything myself, no frameworks

    Comments on this post

    • kicken disagrees : [0] Do not quote entire posts. Only a part you are responding to directly, if any at all. We all have scrollbars we can use.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    397
    Rep Power
    75
    Try making the timestamp parameter part of the params string, instead of part of url.


    No it's not 'awesome' - it's just code.
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,994
    Rep Power
    4058
    I would remove all the cache stuff, as it doesn't really apply to a post request. After that:

    xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
    try using the mime type all lowercase, as in:
    Code:
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    It didnt seem to matter for me when posting to a php script but I believe it's supposed to be lowercase anyway.

    Other than that, get a sniffer like Fiddler and watch the requests so you can see what IE is sending, maybe that will help narrow it down.

    Lastly, if your going to post in multiple sites, you could at least link them up so people know if they should bother spending time looking into your issue when it may have been answered elsewhere:
    ref: http://stackoverflow.com/questions/2...ars-in-ie7-ie8
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo