從客戶端JavaScript實現AJAX/HTTP請求:http.js
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)) }
}); 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!