emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評(píng)論 :: 2 Trackbacks
          * 用xmlhttp發(fā)送請(qǐng)求的時(shí)候,如果服務(wù)器返回的是2xx,4xx或者5xx狀態(tài),是可以從status屬性獲得狀態(tài)碼加以判斷的,但是如果返回的是3xx狀態(tài)(redirects),那么IE會(huì)自動(dòng)跟隨redirects轉(zhuǎn)向到新的請(qǐng)求,xmlhttp無法攔截住這類status加以判斷。所以用用Ajax技術(shù)玩http status要注意3xx頭。
          1xx類的狀態(tài)(Intermediate)還沒有碰到過,不是很關(guān)心。
          * 服務(wù)器返回的http頭里面如果帶 Set-Cookie ,xmlhttp在接收的同時(shí)是可以自動(dòng)設(shè)置cookie的。
          posted on 2006-02-15 10:38 emu 閱讀(1864) 評(píng)論(9)  編輯  收藏

          評(píng)論

          # re: AJAX的小經(jīng)驗(yàn),有個(gè)疑問 2006-02-16 12:34 漂漂
          代碼如下
          function A(){
          this.createRequest();
          }
          A.prototype.buildSoap = function(){
          //創(chuàng)建一個(gè)soap,命名為this.soap
          }
          A.prototype.createRequest = function(){
          //Use an XMLHttpRequest,命名為this.request
          ...
          var url="...";
          this.request.open("POST",url,true);
          this.request.onreadystatechange= this.handleResponse;
          this.buildSoap();
          this.request.send(this.soap);
          }
          A.prototype.handleResponse= function(){
          if (this.request.readyState == 4){
          if (this.request.status == 200){
          ...
          }
          }
          }
          代碼運(yùn)行后,報(bào)錯(cuò)“this.request.readyState 為空或不是對(duì)象”,請(qǐng)問這種JS對(duì)象編程時(shí),應(yīng)該怎樣來寫?謝謝  回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 13:02 emu
          //Use an XMLHttpRequest,命名為this.request

          這個(gè)雙語合壁的注釋都?jí)蚰涿盍耍瑢?shí)現(xiàn)代碼還略過。看不出問題。  回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 13:34 漂漂
          呵呵,真不好意思-_-!,由于找不到你的其他聯(lián)系方式,又怕寫太多,就成了剛才那樣子,完整的測試代碼如下:

          <HTML>
          <HEAD>
          <TITLE> New Document </TITLE>
          </HEAD>
          <script language="javascript" type="text/javascript">
          function startTest(){
          var ob= new A();
          ob.createRequest();
          }
          function A(){

          }
          A.prototype.buildSoap = function(){
          //創(chuàng)建一個(gè)soap,命名為this.soap
          var xmlString = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
          "<SOAP:Header/>"+
          "<SOAP:Body>"+
          "<Authenticate>"+
          "<username></username>"+
          "<password></password>"+
          "</Authenticate>"+
          "</SOAP:Body>"+
          "</SOAP:Envelope>";
          this.soap = new ActiveXObject("Microsoft.XMLDOM");
          this.soap.async = false;
          this.soap.loadXML(xmlString);
          this.soap.selectSingleNode(".//username").text = "administrator";
          this.soap.selectSingleNode(".//password").text = "123456";
          }
          A.prototype.createRequest = function(){
          //Use an XMLHttpRequest,命名為this.request
          try {
          this.request = new XMLHttpRequest();
          } catch (trymicrosoft) {
          try {
          this.request = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (othermicrosoft) {
          try {
          this.request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (failed) {
          this.request = false;
          }
          }
          }
          if (!this.request)
          alert("Error initializing XMLHttpRequest!");
          var url="...";//訪問地址,略
          this.request.open("POST",url,true);
          this.request.onreadystatechange= this.handleResponse;
          this.buildSoap();
          this.request.send(this.soap);
          }
          A.prototype.handleResponse= function(){
          if (this.request.readyState == 4){
          if (this.request.status == 200){
          alert("服務(wù)完成");
          }
          }
          }
          </script>
          <body onload="startTest()">
          </body>
          </HTML>
          代碼運(yùn)行后,報(bào)錯(cuò)“this.request.readyState 為空或不是對(duì)象”,
          其實(shí)碰到的問題主要是在處理onreadystatechange事件的函數(shù)上,由于該函數(shù)是我自己構(gòu)造的對(duì)象A的函數(shù),我不想把函數(shù)寫成全局的。
            回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 13:44 漂漂
          用了雙語合壁的注釋,比較著急中,確實(shí)沒在意這些問題,呵呵  回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 14:17 emu
          你都沒有搞清楚原型對(duì)象和對(duì)象實(shí)例的差別,就this來this去的。

          this.request.onreadystatechange= this.handleResponse;

          handleResponse是原型對(duì)象上的方法,不是對(duì)象實(shí)例的方法,而this.request是對(duì)象實(shí)例里面的子對(duì)象。

          你想要在handleResponse里面通過this引用對(duì)象實(shí)例,可是原型對(duì)象的方法里面的this指向的是原型對(duì)象而不是實(shí)例對(duì)象,request是實(shí)例對(duì)象的屬性,原型對(duì)象沒有request當(dāng)然要報(bào)錯(cuò)了。

          this.request.send(this.soap);
          好好看看xmlhttp的api吧,有接受xmldom對(duì)象作為參數(shù)的send方法嗎?
            回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 14:41 emu
          幫你改一個(gè)能運(yùn)行的版本,但是具體的業(yè)務(wù)和功能我就不得而知了。
          注意到你嘗試創(chuàng)建非IE瀏覽器的 XMLHttpRequest 對(duì)象,可是buildSoap方法使用了Microsoft.XMLDOM對(duì)象已經(jīng)注定了這個(gè)程序不能在非IE瀏覽器上運(yùn)行了,寫了也是白寫,但是還是給你保留了。

          <HTML>
          <HEAD>
          <TITLE> SOAP test by emu </TITLE>
          <META NAME="Author" CONTENT="emu">
          </HEAD>
          <script language="javascript" type="text/javascript">
          function startTest(){
            var ob= new A();
            ob.createRequest();
          }
          function A(){ }
          A.prototype.buildSoap = function(){
            //創(chuàng)建一個(gè)soap,命名為this.soap
            var xmlString = "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
            "<SOAP:Header/>"+
            "<SOAP:Body>"+
            "<Authenticate>"+
            "<username></username>"+
            "<password></password>"+
            "</Authenticate>"+
            "</SOAP:Body>"+
            "</SOAP:Envelope>";
            this.soap = new ActiveXObject("Microsoft.XMLDOM");
            this.soap.async = false;
            this.soap.loadXML(xmlString);
            this.soap.selectSingleNode(".//username").text = "administrator";
            this.soap.selectSingleNode(".//password").text = "123456";
          }
          A.prototype.createRequest = function(){
            var r;
            try {r=new XMLHttpRequest();}catch(e){}
            if(!r)  try {r=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
            if(!r)  try {r=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
            if (!r) {
              alert("Error initializing XMLHttpRequest!");
              return;
            }
            var url="http://www.aygfsteel.com";
            r.open("POST",url,true);
            r.onreadystatechange= function(){
              if(r.readyState==4&&r.status==200)
                alert("服務(wù)完成");
            };
            this.buildSoap();
            r.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
            r.setRequestHeader("If-Modified-Since","0");
            r.send(this.soap.xml);
          }
          </script>
          <body onload="startTest()">
          </body>
          </HTML>
            回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 14:47 漂漂
          Send方法的參數(shù)類型是Variant,可以是字符串、DOM樹或任意數(shù)據(jù)流。

          看了你的解答后,修改了一下代碼,程序運(yùn)行達(dá)到了預(yù)期效果,但是感覺比較繁瑣

          將原來的“this.request.onreadystatechange= this.handleResponse; ”修改成“var aaa=this; this.request.onreadystatechange=function(){get(aaa)};”
          增加一個(gè)全局函數(shù):
          function get(aaa){
          aaa.handleResponse();
          }
          由于onreadystatechange響應(yīng)函數(shù)不能帶參數(shù),所以不得不寫成“function(){get(aaa)}”,而且這樣可以把我需要的this保留并傳遞。

          有沒有什么更簡潔的寫法呢?謝謝:)  回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-16 14:58 漂漂
          哦 或者寫成“var aaa=this;this.request.onreadystatechange = function(){aaa.handleResponse()};”  回復(fù)  更多評(píng)論
            

          # re: AJAX的小經(jīng)驗(yàn) 2006-02-17 11:25 emu
          如果不是真的需要繼承重用和多態(tài)的話,我不建議使用js來進(jìn)行“面向?qū)ο蟆本幊獭S谩盎趯?duì)象”的語言來做“基于對(duì)象”的設(shè)計(jì)和編碼,感覺要比較自然些。
          用原型對(duì)象模擬的面向?qū)ο箝_發(fā),在實(shí)現(xiàn)多態(tài)和繼承的時(shí)候和傳統(tǒng)的面向?qū)ο笳Z言始終有各種差異,開發(fā)的時(shí)候要對(duì)js的對(duì)象體系有充分的理解和保持非常清晰的死路。
          也有一些很優(yōu)秀的面向?qū)ο骿s框架,但是我還是擔(dān)心,解決一個(gè)問題,帶來更多的問題。  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 鸡泽县| 大余县| 麻阳| 东丽区| 汽车| 上林县| 兴仁县| 邛崃市| 太仆寺旗| 台南市| 贺兰县| 商都县| 鄂尔多斯市| 深圳市| 新民市| 常山县| 柳州市| 张家港市| 福泉市| 同仁县| 抚宁县| 元江| 安仁县| 石泉县| 株洲市| 新邵县| 临漳县| 九寨沟县| 营口市| 美姑县| 徐汇区| 泽库县| 平远县| 舒兰市| 砚山县| 罗甸县| 临潭县| 那坡县| 喀喇| 休宁县| 睢宁县|