├── README.md ├── LICENSE.txt ├── HKwest.js └── docs └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # HKwest.js 2 | HKwest.js is a Simple & Minimalist JavaScript AJAX & HTTP/REST Requests Library. http://www.oyedelehammed.ml/HKwest.js 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 - Current 4 | Oyedele Hammed Horlah, http://www.oyedelehammed.ml 5 | 6 | 7 | All rights reserved. 8 | 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining 11 | a copy 12 | 13 | of this software and associated documentation files (the 14 | "Software"), to deal 15 | 16 | in the Software without restriction, including 17 | without limitation the rights to use, copy, modify, merge, publish, 18 | distribute, sublicense, and/or sell 19 | 20 | copies of the Software, and to 21 | permit persons to whom the Software is furnished to do so, subject to 22 | the following conditions: 23 | 24 | The above copyright notice and this permission notice shall be 25 | included in all copies or substantial portions of the Software. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 28 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 29 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 30 | NONINFRINGEMENT. 31 | 32 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 34 | OF CONTRACT, TORT OR 35 | 36 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 37 | WITH THE SOFTWARE OR 38 | THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /HKwest.js: -------------------------------------------------------------------------------- 1 | /** HKwest | AJAX/HTTP/CORS Library 2 | * @author Oyedele Hammed Horlah 3 | * @see http://www.github.com/devHammed/HKwest.js 4 | * @description HKwest is a simple and minimalist AJAX, HTTP Requests and CORS Request Javascript Library. 5 | * @since October 8, 2017 6 | * @version 2.0 7 | */ 8 | 9 | function HKwest( opt ) { 10 | var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new XDomainRequest(), 11 | serialize = function ( obj ) { 12 | if ( typeof obj !== 'object' ) return obj; 13 | var res = []; 14 | for ( var key in obj ) 15 | res.push( key + '=' + encodeURIComponent( obj[ key ] ) ); 16 | return res.join( '&' ); 17 | }, 18 | type = (opt.contentType) ? opt.contentType : 'application/www-form-url-encoded'; 19 | xhr.open( opt.type || 'GET', opt.url, true ); 20 | xhr.withCredentials = true; 21 | xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' ); 22 | if ( opt.headers ) { 23 | for ( var header in opt.headers ) 24 | xhr.setRequestHeader( header, opt.headers[ header ] ); 25 | } 26 | xhr.setRequestHeader( 'Content-type', type ); 27 | if ( opt.mime ) 28 | xhr.overrideMimeType( opt.mime ); 29 | if ( opt.returnType ) 30 | xhr.responseType = opt.returnType; 31 | if ( opt.auth ) 32 | xhr.setRequestHeader( 'Authorization', 'Basic ' + btoa( opt.auth ) ); 33 | xhr.onload = function () { 34 | if ( xhr.readyState != 4 ) return; 35 | opt.done && opt.done( xhr ); 36 | } 37 | xhr.send( serialize( opt.data ) || null ); 38 | } -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | HKwest.js - Simple and Minimalist AJAX/HTTP Requests Javascript Library 8 | 9 | 10 | 11 | 12 | 13 |
14 |

HKwest.js - Simple AJAX/HTTP Requests Library

15 |
16 |
17 | Fork on Github 18 |
19 |

HKwest.js AJAX/HTTP Library

20 |
21 | HKwest.js is a simple and minimalist Javascript Library for AJAX, HTTP Requests and CORS Requests. 22 |
23 |
24 |

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 |
30 | <script>
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 |
39 |
40 | Below is an example POST requests to HTTPBIN.org
41 |
42 | <script>
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 |
61 |
62 |
63 |

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 |
67 |

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 |
77 |
78 |
79 |
80 |
81 | Thanks, Check out my other projects here. 82 |
83 |

84 | 87 | 88 | 89 | --------------------------------------------------------------------------------