Rest is a popular concept now, rest has become a more and more popular application on the web, more and more rest-based Web services, including Twitter, micro-blog is a rest as an external API, previously I have described the "Rest architecture based Web Service Design ", and given some server-side and client code, with the wide application of JavaScript, I'm here to give a lightweight JavaScript-based rest client framework.
This JavaScript client primarily uses the XMLHttpRequest object to retrieve and modify resources by manipulating get, put, post, and delete over HTTP to the server. It is worth noting that JavaScript is limited to cross-domain access due to security considerations, so when calling XMLHttpRequest, you should pay attention to cross-domain access issues, such as using dynamic files from the same domain as proxies, or other ways to circumvent cross-domain access issues. The code I'm giving here is mostly based on my previous code, and its client JavaScript code looks like this:
function HttpGet (URL, method, data) {
var xmlhttp;
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.open (method, url + "?" + data, false);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
Xmlhttp.setrequestheader ("Content-length", data.length);
Xmlhttp.send (NULL);
if (XMLHTTP. Status =) return xmlhttp.responsetext;
}
function HttpPost (URL, method, data) {
var xmlhttp;
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.open (method, URL, false);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
Xmlhttp.setrequestheader ("Content-length", data.length);
Xmlhttp.send (data);
if (XMLHTTP. Status =) return xmlhttp.responsetext;
}
function Httpput (URL, method, data) {
var xmlhttp;
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.open (method, URL, false);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
Xmlhttp.setrequestheader ("Content-length", data.length);
Xmlhttp.send (data);
if (XMLHTTP. Status =) return xmlhttp.responsetext;
}
function Httpdelete (URL, method, data) {
var xmlhttp;
XMLHTTP = new XMLHttpRequest ();
Xmlhttp.open (method, url + "?" + data, false);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
Xmlhttp.setrequestheader ("Content-length", data.length);
Xmlhttp.send (NULL);
if (XMLHTTP. Status =) return xmlhttp.responsetext;
}
function Test () {
document.write (HttpGet ("http://localhost/rest/service.asp", "Get", "Do=get"));
document.write (HttpGet ("http://localhost/rest/service.asp", "POST", "Do=post"));
document.write (HttpGet ("http://localhost/rest/service.asp", "put", "do=put"));
document.write (HttpGet ("http://localhost/rest/service.asp", "DELETE", "Do=delete"));
}
I use this code here to write a simple application example, is to manage the application of Twitter friends, you can download it here, because the problem of cross-domain access, this JavaScript only support IE for local use.