从客户端JavaScript实现AJAX/HTTP请求:http.js

jopen 10年前

http.js能够从客户端JavaScript实现AJAX/HTTP请求。

http.js 提供了一种超级简单,超级简易的方式来实现AJAX/HTTP请求 。它基本上是一个很小的几个功能,以简化XMLHttpRequest的调用。

Usage

http.js currently all HTTP methods. GET and POST requests are supported in short-hand form.

Pass in your options to the request function (all are optionall; defaults are shown):

var options = {      method: "GET",               // The method to use      url: "./",                   // The URL to request      async: true,                 // Whether to make the request asynchronously      data: null,                  // Any data you'd like to send      contentType: "text/plain",   // The MIME type of the content you're sending      onload: function() { }       // Your onload callback function  }

Or use the shorthand functions get and post, which can take in a similar options object but without the need for a method.

Example usage

We can try a simple GET request:

http.get({      url: "http://reqr.es/api/users",      onload: function() { console.log(JSON.parse(this.responseText)) }  });

Or a POST request:

var data = { name: "http.js" }  http.post({      url: "http://reqr.es/api/awesome-stuffs",      data: JSON.stringify(data),      contentType: "application/json",      onload: function() { console.log(JSON.parse(this.responseText)) }  });

Even a random DELETE:

http.request({      method: "DELETE",      url: "http://reqr.es/api/users/2",      onload: function() { console.log(JSON.parse(this.status)) }  });

项目主页:http://www.open-open.com/lib/view/home/1417952415558