淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

体验ajax 程序

上一篇 / 下一篇  2008-05-02 13:12:28 / 个人分类:前端性能测试

 

经过测试在IE6 以及firefox2.0 都可以正常运行的ajax程序

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ajax demo</title>

<scrīpt type="text/javascrīpt" language="javascrīpt">
 var httpRequest =false;
    function makeRequest(url) {       
 document.getElementById("result").innerHTML="";
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        }
        else if (window.ActiveXObject) { // IE
  var versions = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP','MSXML.XMLHTTP', 'Msxml2.XMLHTTP.7.0',

'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', ];
   for(var i=0; i<versions.length; i++) {
        try {
       httpRequest = new ActiveXObject(versions[i]);     
    } catch(e) {}
   }
         }
        if (!httpRequest) {
            alert('Cannot create an XMLHTTP instance');
            return false;
        }
 
        httpRequest.onreadystatechange = alertContents;   //function() { alertContents(httpRequest); };
        httpRequest.open('GET', url, true);
 httpRequest.setRequestHeader('Cache-Control','no-cache');
        httpRequest.send('');
    }

    function alertContents(){
        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                //alert(httpRequest.responseText);
  document.getElementById("result").innerHTML =httpRequest.responseText;
            } else {
               document.getElementById("result").innerHTML='There was a problem with the request.';
            }
        }
    }
</scrīpt>
</head>
<body>
<span
    style="cursor: pointer; text-decoration: underline"
    ōnclick="makeRequest('http://127.0.0.1:3000/my_test/')">
        Make a request
</span>
<br>
<span id="result" style="" >
</body>
</html>

 

在本地启动mongrel服务器侦听3000端口


TAG: ajax XMLHttpRequest ActiveXObject 前端性能测试

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar