HKwest.js AJAX/HTTP Library
20 |21 | HKwest.js is a simple and minimalist Javascript Library for AJAX, HTTP Requests and CORS Requests. 22 |
Request Examples
25 |26 | HKwest.js supports these HTTP Methods (GET, POST, PUT, PATCH, DELETE, OPTIONS, TRACE, CONNECT) and can be use to develop AJAX and RESTful projects.
27 | HKwest.js would return XMLHttpRequest/XDomainRequest object to your callback function after successful request so you will have access to all Javascript Native methods e.g responseText, responseXML, status, statusText, getAllResponseHeaders, getResponseHeader(header) etc.
28 | Below is an example GET request to HTTPBIN.org site:
29 |
31 | HKwest({
32 | url: 'http://httpbin.org/get?q=name',
33 | done: function (xhr) {
34 | // do something with XHR object
35 | // alert(xhr.responseText);
36 | }});
37 | </script> 38 |
40 | Below is an example POST requests to HTTPBIN.org
41 |
43 | HKwest({
44 | url: 'http://httpbin.org/post',
45 | type: 'POST',
46 | data: {
47 | "name": "Oyedele Hammed Horlah",
48 | "age": 17,
49 | "bio": "I'm a Self-taught Computer Programmer and Web Developer"
50 | },
51 | headers: {
52 | "X-Key": "value",
53 | "X-Name": "horlahcoded"
54 | },
55 | done: function (xhr) {
56 | // do something with XHR object
57 | // alert(xhr.responseText);
58 | }});
59 | </script> 60 |
Options and Settings
64 |65 | HKwest.js options are used to configure the request. some of this options are optional and some are required. 66 |
url : Request URL (required)
68 |type : Request Method (optional, default: GET)
69 |data : Request Body, it can be a String or Object (key/value) or JSON String or FormData Object (optional)
70 |headers : Request Headers in Keys and Values Pairs (Object) (optional)
71 |contentType : Request Body Data Type (default: application/www-form-url-encoded, optional)
72 |auth : Request Authentication, username:password String e.g auth: 'username:password' (optional)
73 |mime : Override Response Data Mime(optional)
74 |done : Callback Function, HKwest would pass the XHR Object to this Function so that you would have access to these properties (response, responseText, responseXML, responseURL, responseType, status, statusText, getAllResponseHeaders(), getResponseHeader(header) and readyState). (optional, default: Console Log)
75 |returnType : This would tell XHR the type of response that would be received, it accepts (text, arraybuffer, blob, json, document) values only. (default: text, optional)
76 |78 |