views:

321

answers:

4

Hi Guys new to this site but a big fan. Right the problem. It's come to our attention that sometimes on Internet Explorer the post variable don't come through. This is our basic ajax function

function GetXmlHttpObject(handler){ 
 var objXmlHttp=null

 if (navigator.userAgent.indexOf("Opera")>=0){
  xmlHttp=new XMLHttpRequest();
  xmlHttp.onload=handler
  xmlHttp.onerror=handler
  return xmlHttp;
 }
 if (navigator.userAgent.indexOf("MSIE")>=0){ 
  var strName="Msxml2.XMLHTTP"
  if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
   strName="Microsoft.XMLHTTP"
  } try { 
   objXmlHttp=new ActiveXObject(strName)
   if(handler == null) {
    handler = function() {}
   }
   objXmlHttp.onreadystatechange=handler 
   return objXmlHttp
  } catch(e) { 
   return 
  } 
 } 
 if (navigator.userAgent.indexOf("Mozilla")>=0){
  objXmlHttp=new XMLHttpRequest()
  objXmlHttp.onload=handler
  objXmlHttp.onerror=handler 
  return objXmlHttp
 }
}

and here is the call that uses it

 params = "object_type="+object_type+"&object_id="+object_id;
 xmlHttp_comment_notifyreset = GetXmlHttpObject(notification_reset_helper);//fails on safari 1
 xmlHttp_comment_notifyreset.open("POST", url , true);


 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);

Right basically the object_type,object_id don't get sent despite being there. Like I say it looks to be just a IE7/8 issue that sometimes happens.

I thought that it might be a caching problem. But what we do is we have one function.js file however evertime we make a change we change the last changed timestamp and use htaccess to get the new file which seems to work. As IE etc treats the file as new in it's cache.

P.S We can't use JQuery or any other frameworks as they are too large to download for our members.

Thanks for your help. Richard

A: 

If they can't download the core jQuery minified or packed version then I think you have problems. It's not that large and the fact that browsers allow for 8 connections now...

It's also available via Google's CDN, see http://code.google.com/apis/ajaxlibs/documentation/

nickyt
Can anyone point out why there is a problem at all?Richard
Richard Housham
The problem is what you're coding has already been done by several client-side frameworks and probably done better.As I said, you can normally grab core functionality of these frameworks from their respective web sites or use CDN references, http://en.wikipedia.org/wiki/Content_delivery_network, to these frameworks.The cars are already built, just learn to steer one.
nickyt
A: 

I think the problem is with recognizing IEs - You put IE6+ in one bag, which isn't right. It changed between ie6 and 7+ as far as I remember. You cant depend on browser names.

Seriously. This is the worst XMLHttpRequest creation code ever. The object creation is done in some great examples on reliable dev sites and in books on AJAX. Where did You get this from?

But please use a framework. There's a whole new world of one line ajax calls waiting for You!!

I'd bet rewriting Your code well using jquery and minifying it (which You probably don't do now) would make the js total weight less than now. Especially that the whole POST usage will take 5 lines of code.

naugtur
A: 

Hi guys I'm still having problems. I'm using

function GetXmlHttpObject(handler){ 
     var objxml = null;  
   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;  

}

As my XML object and

this kinda style for calling the page

var d = new Date();
    var time = d.getTime();
    var url= SITEURL + "/libs/ajax/ajax_site_mood.php?timestamp="+time;

    params = "mood_value="+value;
    xmlHttp_site_mood = GetXmlHttpObject(null);//fails on safari 1
    xmlHttp_site_mood.open("POST", url , true);

    xmlHttp_site_mood.setRequestHeader("Cache-Control", "no-cache");
    xmlHttp_site_mood.setRequestHeader("Cache-Control", "no-store");
    xmlHttp_site_mood.setRequestHeader("Cache-Control", "must-revalidate");
    xmlHttp_site_mood.setRequestHeader("Cache-Control", "post-check=0");
    xmlHttp_site_mood.setRequestHeader("Cache-Control", "pre-check=0");
    xmlHttp_site_mood.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");    


    xmlHttp_site_mood.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
    xmlHttp_site_mood.setRequestHeader("Content-Length", params.length);
    xmlHttp_site_mood.setRequestHeader("Connection", "close");
    xmlHttp_site_mood.send(params);

I'm changing the request for the library every time it changes based off it's last updated time. But doesn't seem to want to always work. Any clues. I'm curious also as to what jquery headers it sends when it calls an post and uses it's cache:false Thanks for your help. Richard

Richard Housham
A: 

Found the answer. Right ok the stuff above is all good get a good XMLHTTP object thing the one above looks good. The extra set request header is also good. But the real problem is on this page here http://qfox.nl/notes/1

Richard Housham