hi all, it's my code , the function will be called when button onclick , firefox and chrome is running successful , but it's can't work in IE , please someone else can help me ! function loadAjax(aaa,bbb,ccc){ if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ /*if (xmlhttp.readyState==4 && xmlhttp.status==200){ alert(xmlhttp.responseText); //document.getElementById(str).innerHTML=xmlhttp.responseText; }*/ } xmlhttp.open(aaa,bbb,ccc); xmlhttp.send(); } Code (markup):
There's a useful piece of example code over on Webmaster World that shows how to get a cross-browser XMLHttpRequest object.
Try using jQuery's ajax() function, it's easier and it's cross-browser compatible. function loadAjax(aaa, bbb, ccc) { $.ajax({ url: "yourPhpPage.php", data: 'aaa='+aaa+ '&bbb='+bbb+ '&ccc='+ccc, success: function(data){ alert(data); }); } Code (markup): Remember to load the jQuery library. The easiest way to do that is <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js"></script> <script type="text/javascript"> if (typeof jQuery == "undefined") { document.write(unescape("%3Cscript src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'%3E%3C/script%3E")); } if (typeof jQuery == "undefined") { document.write(unescape("%3Cscript src='/js/jquery-1.6.2.min.js' type='text/javascript'%3E%3C/script%3E")); } </script> Code (markup): The last line loads it from your server, if both Microsoft and Google are down. (If that happens, your user probably lost his internet connection, but just in case MS and Google both went down at the same time, it can't hurt.)
DO NOT USE JQUERY!!! ABSOLUTELY DO NOT! As for your code, you have most of it commented out, which is your problem, if i were you, i would make the function a bit more like this: <script> //notice the parameters //method is the request method, such as GET or POST //the url is the url that you want //the call back is the function that it calls when a response is made function loadAjax(method, url, callback) { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { //this returns the response to the call back function, and sends it as a parameter callback(xmlhttp.responseText); } } //the parameters //1. is Method //2. is the Url //3. is a boolean for Asynchronous or not, //if the boolean is false, it will wait until a response and then finish the end of the function //we do not want this, because it could freeze your browser if set to false xmlhttp.open(method,url,true); xmlhttp.send(); } </script> Code (markup): and then as for the usage <script> function processResponse(response) { alert(response); } loadAjax('GET', 'MyUrl.php', processResponse); </script> Code (markup):
In your code xmlhttp=new XMLHttpRequest(); fails immediately in IE (there's no XMLHttpRequest() in IE), and the rest of the code never gets executed. Try this instead: function GetXmlHttpObject() { var xmlHttp=null; try { [COLOR=#00ff00]// Firefox, Opera 8.0+, Safari[/COLOR] xmlHttp=new XMLHttpRequest(); } catch (e) { [COLOR=#00ff00]// Internet Explorer[/COLOR] try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } Code (markup): If it fails at that point, it's some browser that can't support AJAX, and there's no code you can write to fix it. ( I don't know of any current ones, but there are still people running Windows 98.) (I don't know any reason for the "DO NOT USE JQUERY!!!", other than the fact that I told you to. Some of the best and largest commercial sites use it.)
lol, that's because they don't care about efficiency, they care about speed. PS, as to your answer, new ActiveXObject("Microsoft.XMLHTTP"); is only required for IE under 7. lol, your a dumb troll, you'd have to be a complete moron to argue with the code i posted up.
The OP didn't mention that he is using (or willing to use) jQuery. So the first point should be to give a pure JavaScript solution. Now back to you problem. The reason this doesn't work on Internet Explorer, is that IE doesn't support cross-domain AJAX requests (well, it does, but not in the same way other browsers do). This mean that you can make AJAX calls only to files in your domain name. jQuery doesn't even help, because it doesn't have a support for this particular issue as of the latest version. ActiveX is old, and should not be used any more. As an alternative, Microsoft introduced XDR as of IE 8 (http://msdn.microsoft.com/en-us/library/dd573303(v=vs.85).aspx). If you have to support IE7/6 users, then you can do a fancy trick (which will have cross browser support with just one code); It's by creating a script in your server that loads the data from the target URL. This will consume some server side resources