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