JavaScript-based REST client framework

Source: Internet
Author: User
Tags web services

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.

Related Article

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.