(轉)ExtJS之Ext.Ajax.request用法詳解


轉自:http://javacrazyer.iteye.com/blog/641112/

  1. <span style="font-size: medium;">Ext.Ajax.request({     
  2.        url:'findbyid.action',  
  3.        params:{  
  4.         id:cell.getId()  
  5.         },  
  6.         success: function(resp,opts) {   
  7.                              var respText = Ext.util.JSON.decode(resp.responseText);                                                   
  8.                              name=respText.name;  
  9.                              oid=respText.id;    
  10.                           findbyid(graph,cell,oid,name);  
  11.                              //Ext.Msg.alert('錯誤', respText.name+"====="+respText.id);   
  12.                      },   
  13.                      failure: function(resp,opts) {   
  14.                              var respText = Ext.util.JSON.decode(resp.responseText);   
  15.                              Ext.Msg.alert('錯誤', respText.error);   
  16.                       }     
  17.          
  18.       });</span>  

在Ext開發過程中,基本上時刻需要用到異步請求,那么這個請求到底是怎么執行的呢,我們接下來來探討下

首先:Ext.Ajax類繼承了Ext.data.Connection,為Ajax的請求提供了最大靈活性的操作方式

再簡單請求基礎上我們加上一個使用的

 

說明的是這種請求通常都是放在觸發某個事件的處理方法中的

url:就是我們要請求的路徑

params:里面的參數用逗號隔開,就是我們要發出請求帶的參數

success:是服務器處理成功返回

failure:是服務器處理失敗返回

重點講的就是如何處理返回值信息,我們的resp這個參數就顯得非常重要了

resp是Ext構造的一個返回結果對象,如服務器端返回“this is a test!”(可以通過throw new Exception("this is a test!")簡單實現)。那么返回將是
如下內容:
  1. <span style="font-size: medium;">tId.1  
  2. status.200  
  3. statusText.OK  
  4. getResponseHeader.[object Object]  
  5. getAllResponseHeaders.Server: Apache-Coyote/1.1  
  6. Content-Type: text/html;charset=GBK  
  7. Content-Language: zh-CN  
  8. Content-Length: 108  
  9. Date: Wed, 31 Oct 2007 12:51:23 GMT  
  10. responseText.  
  11. <html>  
  12. <head>  
  13. <title>錯誤</title>  
  14. </head>  
  15. <body>  
  16.   <h1>錯誤:this is a test!</h1>  
  17. </body>  
  18. </html>  
  19. responseXML.  
  20. argument.undefined</span>  
 
 
從上面結果可以看出來,最開始是一些狀態屬性,我們也不常用,不管他。里面真正常用的是responseText與responseXML兩個屬性,那么這里面的responseText內容又被Ext用html包裝了,但使用Ext.MessageBox展示出來正合適;reponseXML將在服務器端返回“text/xml”類型時使用。若服務器端返回是“text/json”類型時,客戶端需要使用obj= Ext.util.JSON.decode(result.responseText);進行構造json對象,然后就可以正常使用了
具體操作返回值 我們用JSON就這么寫
ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
ServletActionContext.getResponse().getWriter().write("{success:true,info:'更新信息成功',name:'" + oo.getName() + "',id:'" + id + "'}");
顯然我這里返回的是JSON的值了(記住里面的屬性值一定要加單引號)
 var respText = Ext.util.JSON.decode(resp.responseText); 
這個就可獲得返回結果對象,要使用屬性的話respText.id等都可直接用了
 
說到這里如果還想對這里面其他配置感興趣的話可以參考下面的語句
  • url : String/Function (Optional)
    (可選項)發送請求的url,默認為配置的url。 若為函數類型那么其作用域將由配置項 scope所指定。默認為配置好的URL。 The URL to which to send the request, or a function to call which returns a URL string. The scope of the function is specified by the  scope option. Defaults to configured URL.
  • params : Object/String/Function (可選項)(Optional)
    一包含屬性的對象(這些屬性被用作request的參數)或一個編碼后的url字串或一個能調用其中任一一屬性的函數。 若為函數類型那么其作用域將由配置項 scope所指定。 An object containing properties which are used as parameters to the request, a url encoded string or a function to call to get either. The scope of the function is specified by the  scope option.
  • method : String (可選項)(Optional)
    該請求所用的http方面,默認值為配置的方法,或者當沒有方法被配置時,如果沒有發送參數時用get,有參數時用post。 The HTTP method to use for the request. Defaults to the configured method, or if no method was configured, "GET" if no parameters are being sent, and "POST" if parameters are being sent. Note that the method name is case-sensitive and should be all caps.
  • callback : Function (可選項)(Optional)
    該方法被調用時附上返回的http response對象。不管成功還是失敗,該回調函數都將被調用,該函數中傳入了如下參數: The function to be called upon receipt of the HTTP response. The callback is called regardless of success or failure and is passed the following parameters:
    • options : Object
      >請求所調用的參數。The parameter to the request call.
    • success : Boolean
      請求成功則為true。True if the request succeeded.
    • response : Object
      包含了返回數據的xhr對象。The XMLHttpRequest object containing the response data. See http://www.w3.org/TR/XMLHttpRequest/ for details about accessing elements of the response.
  • success: Function (可選項)(Optional)
    該函數被調用取決於請求是否成功。該回調函數被傳入如下參數: The function to be called upon success of the request. The callback is passed the following parameters:
    • response : Object
      包含數據的xhr對象。The XMLHttpRequest object containing the response data.
    • options : Object
      請求所調用的參數。The parameter to the request call.
  • failure : Function (可選項)(Optional)
    該函數被調用取決於請求失敗。該回調函數被傳入如下參數: The function to be called upon failure of the request. The callback is passed the following parameters:
    • response : Object
      包含數據的xhr對象。 The XMLHttpRequest object containing the response data.
    • options : Object
      請求所調用的參數。 The parameter to the request call.
  • scope : Object (可選項)(Optional)
    回調函數的作用域:回調函數"this"對象指針。默認值為瀏覽器窗口。 The scope in which to execute the callbacks: The "this" object for the callback function. If the  url, or  params options were specified as functions from which to draw values, then this also serves as the scope for those function calls. Defaults to the browser window.
  • form : Element/HTMLElement/String (可選項)(Optional)
    用來壓入參數的一個 <form>元素或 <form>的標識。 The  <form> Element or the id of the  <form> to pull parameters from.
  • isUpload : Boolean (可選項)(Optional)
    如果該form對象是上傳form,為true(通常情況下會自動探測)
  
  • headers : Object (可選項)(Optional)
    為請求所加的請求頭。 Request headers to set for the request.
  • xmlData : Object (可選項)(Optional)
    用於發送的xml document。注意:它將會被用來在發送數據中代替參數任務參數將會被追加在url中。 XML document to use for the post. Note: This will be used instead of params for the post data. Any params will be appended to the URL.
  • jsonData : Object/String (可選項)(Optional)
    JSON data to use as the post. Note: This will be used instead of params for the post data. Any params will be appended to the URL.
  • disableCaching : Boolean (可選項)(Optional)
    設置為True,則添加一個獨一無二的cache-buster參數來獲取請求。 True to add a unique cache-buster param to GET requests   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM