emu in blogjava

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

          評論

          # re: AJAX的小經(jīng)驗,有個疑問 2006-02-16 12:34 漂漂
          代碼如下
          function A(){
          this.createRequest();
          }
          A.prototype.buildSoap = function(){
          //創(chuàng)建一個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){
          ...
          }
          }
          }
          代碼運行后,報錯“this.request.readyState 為空或不是對象”,請問這種JS對象編程時,應(yīng)該怎樣來寫?謝謝  回復(fù)  更多評論
            

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

          這個雙語合壁的注釋都夠莫名其妙了,實現(xiàn)代碼還略過。看不出問題。  回復(fù)  更多評論
            

          # re: AJAX的小經(jīng)驗 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)建一個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>
          代碼運行后,報錯“this.request.readyState 為空或不是對象”,
          其實碰到的問題主要是在處理onreadystatechange事件的函數(shù)上,由于該函數(shù)是我自己構(gòu)造的對象A的函數(shù),我不想把函數(shù)寫成全局的。
            回復(fù)  更多評論
            

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

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

          this.request.onreadystatechange= this.handleResponse;

          handleResponse是原型對象上的方法,不是對象實例的方法,而this.request是對象實例里面的子對象。

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

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

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

          <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)建一個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ù)  更多評論
            

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

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

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

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

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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 沧源| 汾西县| 南木林县| 惠水县| 隆安县| 黄浦区| 桓仁| 密山市| 洛隆县| 娱乐| 临江市| 兴山县| 定安县| 息烽县| 谢通门县| 健康| 宝坻区| 达拉特旗| 平陆县| 清苑县| 筠连县| 炎陵县| 青浦区| 蒙自县| 新安县| 昔阳县| 甘泉县| 神农架林区| 道孚县| 七台河市| 峨边| 中山市| 侯马市| 湖南省| 西城区| 建湖县| 永康市| 长沙县| 丽江市| 特克斯县| 积石山|