Javascript-based rest client framework

Source: Internet
Author: User
    This article introduces a javascript-based rest client framework. This is a lightweight framework. The 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.

Currently, rest is a popular concept. Rest has become an increasingly common web application, and there are more and more rest-based Web services, including
Twitter and other micro-blogs use rest as their external APIs. I have previously introduced "Rest-based Web
Service design, and provides some server and client code. With the wide application of JavaScript, I will give a lightweight JavaScript-based
Rest client framework.

This JavaScript client mainly uses the XMLHTTPRequest object to perform get, put, post, and
Delete to retrieve and modify resources. It is worth noting that, due to security considerations, JavaScript is restricted by the cross-origin access capability, so when calling
When using XMLHttpRequest, you should pay attention to cross-origin access issues, such as using dynamic files in the same domain as a proxy, or other methods to avoid cross-origin access issues. The Code master I provided here
The client JavaScript code is as follows:

 

Function httpget (URL, method, data) {<br/> var XMLHTTP; <br/> XMLHTTP = new XMLHttpRequest (); <br/> XMLHTTP. open (method, URL + "? "+ Data, false); <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); <br/> XMLHTTP. setRequestHeader ("Content-Length", Data. length); <br/> XMLHTTP. send (null); <br/> If (XMLHTTP. status = 200) return XMLHTTP. responsetext; <br/>}</P> <p> function httppost (URL, method, data) {<br/> var XMLHTTP; <br/> XMLHTTP = new XMLHttpRequest (); <br/> XMLHTTP. open (Method, URL, false); <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); <br/> XMLHTTP. setRequestHeader ("Content-Length", Data. length); <br/> XMLHTTP. send (data); <br/> If (XMLHTTP. status = 200) return XMLHTTP. responsetext; <br/>}</P> <p> function httpput (URL, method, data) {<br/> var XMLHTTP; <br/> XMLHTTP = new XMLHttpRequest (); <br/> XMLHTTP. Open (method, URL, false); <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); <br/> XMLHTTP. setRequestHeader ("Content-Length", Data. length); <br/> XMLHTTP. send (data); <br/> If (XMLHTTP. status = 200) return XMLHTTP. responsetext; <br/>}</P> <p> function httpdelete (URL, method, data) {<br/> var XMLHTTP; <br/> XMLHTTP = new XMLHttpRequest (); <br/> XMLHTTP. Open (method, URL + "? "+ Data, false); <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); <br/> XMLHTTP. setRequestHeader ("Content-Length", Data. length); <br/> XMLHTTP. send (null); <br/> If (XMLHTTP. status = 200) return XMLHTTP. responsetext; <br/>}</P> <p> function test () {<br/> document. write (httpget ("http: // localhost/rest/service. ASP "," get "," Do = get "); <br/> document. write (httpget ("http: // localhost/rest/service. ASP "," Post "," Do = post "); <br/> document. write (httpget ("http: // localhost/rest/service. ASP "," put "," Do = put "); <br/> document. write (httpget ("http: // localhost/rest/service. ASP "," delete "," Do = Delete "); <br/>}

 

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.

 

Original article: http://developer.51cto.com/art/200906/128718.htm

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.