基於JavaScript的REST用戶端架構

來源:互聯網
上載者:User

現在REST是一個比較熱門的概念,REST已經成為一個在Web上越來越常用的應用,基於REST的Web服務越來越多,包括Twitter在內的微部落格都是用REST做為對外的API,先前我曾經介紹過“基於REST架構的Web Service設計”,並給出了一些伺服器端和用戶端代碼,隨著JavaScript的廣泛應用,我這裡就給出一個輕量級的基於JavaScript的REST用戶端架構。

這個JavaScript用戶端主要使用了XMLHttpRequest對象來實現通過HTTP對伺服器操作GET、PUT、POST和DELETE以檢索和修改資源。值得注意的是,由於安全方面的考慮,Javascript被限制了跨域訪問的能力,因此在調用XMLHttpRequest的時候,應該注意跨域訪問的問題,比如使用同一個域的動態檔案做代理,或者其他方法避開跨域訪問的問題。我這裡給出的代碼主要是根據我先前的那段代碼修改過來的,其用戶端JavaScript代碼如下所示:

 
  1. function httpGet(url, method, data) {  
  2.     var xmlhttp;  
  3.     xmlhttp = new XMLHttpRequest();  
  4.     xmlhttp.open (method, url + "?" + data, false);  
  5.     xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
  6.     xmlhttp.setRequestHeader ("Content-Length", data.length);  
  7.     xmlhttp.send (null);  
  8.     if (xmlhttp.Status = 200) return xmlhttp.responseText;  
  9. }  
  10.  
  11. function httpPost(url, method, data) {  
  12.     var xmlhttp;  
  13.     xmlhttp = new XMLHttpRequest();  
  14.     xmlhttp.open (method, url, false);  
  15.     xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
  16.     xmlhttp.setRequestHeader ("Content-Length", data.length);  
  17.     xmlhttp.send (data);  
  18.     if (xmlhttp.Status = 200) return xmlhttp.responseText;  
  19. }  
  20.  
  21. function httpPut(url, method, data) {  
  22.     var xmlhttp;  
  23.     xmlhttp = new XMLHttpRequest();  
  24.     xmlhttp.open (method, url, false);  
  25.     xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
  26.     xmlhttp.setRequestHeader ("Content-Length", data.length);  
  27.     xmlhttp.send (data);  
  28.     if (xmlhttp.Status = 200) return xmlhttp.responseText;  
  29. }  
  30.  
  31. function httpDelete(url, method, data) {  
  32.     var xmlhttp;  
  33.     xmlhttp = new XMLHttpRequest();  
  34.     xmlhttp.open (method, url + "?" + data, false);  
  35.     xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");  
  36.     xmlhttp.setRequestHeader ("Content-Length", data.length);  
  37.     xmlhttp.send (null);  
  38.     if (xmlhttp.Status = 200) return xmlhttp.responseText;  
  39. }  
  40.  
  41. function test() {  
  42.     document.write (httpGet("http://localhost/rest/service.asp", "GET", "do=GET"));  
  43.     document.write (httpGet("http://localhost/rest/service.asp", "POST", "do=POST"));  
  44.     document.write (httpGet("http://localhost/rest/service.asp", "PUT", "do=PUT"));  
  45.     document.write (httpGet("http://localhost/rest/service.asp", "DELETE", "do=DELETE"));  
  46. }  

我這裡使用這個代碼編寫了一個簡單的應用例子,就是管理Twitter好友的應用,大家點這裡可以下載使用,因為跨域訪問的問題,這段JavaScript只支援IE在本地使用。

編輯精選】

  1. 如何最佳化JavaScript指令碼的效能
  2. JavaScript控制Excel列印完美解決方案
  3. 用Javascript串連Access資料庫的方法
  4. JavaScript的document和window對象詳解
  5. 淺談JavaScript中物件導向技術的類比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.