@OverWrite BlogJava

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            128 隨筆 :: 0 文章 :: 29 評論 :: 0 Trackbacks

          dojo.io.bind :處理請求取回需要的數據并處理。
                  這個函數是AJAX中最為重要和有用的函數,dojo.io.bind這個類是用來處理客戶端與服務器間通訊的,需要通訊的參數由對象dojo.io.Request所定義,具體通訊的方法則由另外一個對象Transport所提供。Dojo里提供了一個同時兼容IE和Firefox的dojo.io.XMLHTTPTransport,但是這個對象位于dojo.io.BrowserIO,因此,一般require dojo.io.IO時,還應該require dojo.io.BrowserIO
          Usage Example:
           
            

          dojo.io.bind(
              url: 
          "http://localhost/test.html"//要請求的頁面地址 
              mimetype: "text/html"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype,默認為 "text/plain" 
              method:"GET"//默認為"GET" 
              sync: false//默認為異步執(zhí)行 
              useCache: false//默認為不使用頁面緩存,注意這里的緩存并不是瀏覽器的緩存,而是Dojo自身所維護的頁面緩存 
              preventCache: false//默認為啟用瀏覽器緩存,否則將通過自動增加不同的參數來確保瀏覽器緩存失效 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 
              load: function(type, data, evt) { alert(data); }//type should be "load", data is that we wanted 
              error: function(type, error) { alert(error.message); }//error is dojo.io.Error 
              timeout: function(type) { alert("請求超時!"); } 
            }
          ); 


            你也可以用一個handle來處理所有的事件 
            

          dojo.io.bind(
              url: 
          "http://localhost/test.html"//要請求的頁面地址 
              mimetype: "text/html"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 
              handle: function(type, data, evt)
                
          if(type == "load"{ alert(data); } //data is that we wanted 
                else if (type == "error"{ alert(data.message); } //data is the error object 
                else { ; } //other events maybe need handled 
              }
           
            }
          );  

            
            如果沒有在Request中指定所用的transport,則Dojo會自動的在已注冊的transports中尋找能夠處理這個Request的transport,如果不能找到,則返回指定的Request。下面是一個指定了transport的例子: 
            

          dojo.io.bind(
              url: 
          "http://localhost/test.html"//要請求的頁面地址 
              mimetype: "text/html"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 
              transport: "XMLHTTPTransport"
              load: 
          function(type, data, evt) { alert(data); }//type should be "load", data is that we wanted 
              error: function(type, error) { alert(error.message); }//error is dojo.io.Error 
              timeout: function(type) { alert("請求超時!"); } 
            }
          ); 


          你還可以利用bind來得到一個Javas cript所定義的對象(注意mimetype必須要定義為"text/javas cript") 
            

          testObj = dojo.io.bind(
              url: 
          "http://localhost/test.js"//test.js里定義了一個對象 
              mimetype: "text/javas cript"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 handle: function(type, data, evt){ 
                if(type == "load"{ alert(data); } //data is a object or value 
                else if (type == "error"{ alert(data.message); } //data is the error object 
                else { ; } //other events maybe need handled 
              }
           
            });   

           
            下面是一個Post的例子:    
            

          dojo.io.bind(
              url: 
          "http://localhost/test.aspx"//要提交的頁面地址 
              mimetype: "text/html"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 
              method: "POST"
              formNode: dojo.byId(
          "myForm"), //指定提交的Form名稱 
              load: function(type, data, evt) { alert(data); }//type should be "load", data is that we wanted 
              error: function(type, error) { alert(error.message); }//error is dojo.io.Error 
              timeout: function(type) { alert("請求超時!"); } 
            }
          ); 


            另一個Post的例子(without Form to post): 

          dojo.io.bind(
              url: 
          "http://localhost/test.aspx"//要提交的頁面地址 
              mimetype: "text/html"//請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype 
              timeoutSeconds: 3000,  //3秒后超時,如果為0則永不超時 
              method: "POST"
              content: 
          {a: 1, b: 2}//要提交的數據 
              load: function(type, data, evt) { alert(data); }//type should be "load", data is that we wanted 
              error: function(type, error) { alert(error.message); }//error is dojo.io.Error 
              timeout: function(type) { alert("請求超時!"); } 
            }
          ); 


            dojo.io.queueBind
            有時,我們需要一次發(fā)出多個網頁請求,則應該使用dojo.io.queueBind,因為瀏覽器可能只允許同時發(fā)出有限個數的請求,如果是使用dojo.io.bind的話,則有可能會申請不到新的XMLHttp對象而導致出錯。
            用法與dojo.io.bind是一樣的。
          dojo.io.argsFromMap
            用來把對象轉換為URL的參數形式   
          Usage Example:
            dojo.io.argsFromMap({a:1,b:2,c:3}); //will return "c=3&b=2&a=1"
            dojo.io.argsFromMap({name:"名稱",value:"值"},"utf"); //will return "value=?€?&name=????§°", 有中文的話應該指定utf格式,否則dojo.string.encodeAscii返回的編碼是很怪異的
            dojo.io.argsFromMap({a:1,b:2,c:3}, "utf", "c"); //will return "b=2&a=1&c=3",最后一個參數可以控制指定名稱的值出現(xiàn)在最后
          dojo.io.setIFrameSrc
            設置IFrame的Src   
          Usage Example:
            dojo.io.setIFrameSrc(dojo.byId("myFrame"), "http://localhost/test.htm"); //myFrame打開指定的網頁
            dojo.io.setIFrameSrc(dojo.byId("myFrame"), "http://localhost/test.htm", true); //myFrame打開指定的網頁,并覆蓋瀏覽器的歷史記錄
          模塊:dojo.io.BrowserIO
          基本上就提供了dojo.io.XMLHTTPTransport這個對象
          XMLHTTPTransport一般能夠滿足我們的需求,但是其有幾個限制:它不能傳輸文件,不能夠成功執(zhí)行跨域名的遠程請求,并且不支持 file:// 這樣的協(xié)議
          因此,根據應用要求,我們可能會需要選用其它的transport: dojo.io.IframeTransport, dojo.io.repubsubTranport, dojo.io.s criptSrcTransport, ShortBusTransport
          dojo.io.IframeTransport,用法與xmlhttp是一樣的,其優(yōu)點就是可以跨域,不存在任何的安全問題
          如果Request指定的mimetype是text或javas cript,返回的內容應該是放在第一個textarea里的內容,如果指定的mimetype是html,則IFrame里的html則是需要的內容。因為瀏覽器兼容的原因,IframeTransport不能正確處理返回類型為XML的請求。 
          關于Rpc,這個類似于Remoting的東西,也將在以后對其進行介紹。

          posted on 2008-01-31 16:52 vesung 閱讀(2880) 評論(0)  編輯  收藏 所屬分類: Ajax/html
          主站蜘蛛池模板: 扎兰屯市| 深水埗区| 武邑县| 阜阳市| 寻乌县| 鹤山市| 仁怀市| 大新县| 犍为县| 柳州市| 冷水江市| 宝丰县| 玉山县| 望奎县| 东方市| 十堰市| 涡阳县| 镇巴县| 信丰县| 黎平县| 高邮市| 湖南省| 扎兰屯市| 井冈山市| 阆中市| 通山县| 卢氏县| 德昌县| 靖边县| 高阳县| 东宁县| 沂南县| 洪泽县| 池州市| 额济纳旗| 临泽县| 来宾市| 滕州市| 根河市| 镇宁| 毕节市|