JavaScript-based REST client framework

Source: Internet
Author: User

Currently, REST is a popular concept. REST has become an increasingly common Web application, and there are more and more REST-based Web Services, micro-blogs, including Twitter, use REST as their external APIs. I have previously introduced "REST-based Web Service design" and provided some server and client code, with the wide application of JavaScript, a lightweight JavaScript-based REST client framework is provided here.

This JavaScript client mainly uses the XMLHttpRequest object to perform GET, PUT, POST, and DELETE operations on the server through HTTP to retrieve and modify resources. It is worth noting that, due to security considerations, Javascript is restricted by cross-origin access capabilities. Therefore, when calling XMLHttpRequest, you should pay attention to cross-origin access, for example, you can use dynamic files in the same domain as a proxy, or use other methods to avoid cross-origin access. The code I provided here is mainly modified according to my previous code. The client JavaScript code is as follows:

 
 
  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. }  

I used this code to compile a simple application example, that is, to manage Twitter friends. You can download and use it here because of cross-origin access problems, this section of JavaScript only supports local use of IE.

Edit recommendations]

  1. How to optimize the performance of JavaScript scripts
  2. Perfect solution for controlling Excel printing with JavaScript
  3. How to connect to the Access database using Javascript
  4. Document and window objects in JavaScript
  5. Introduction to the simulation of object-oriented technology in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.