1.2.0 • Published 5 years ago
ajaxsimpler v1.2.0
AjaxSimplier
Installation
Install with npm:
$ npm i ajaxsimplerOr, use from unpkg.com:
<script src="https://unpkg.com/ajaxsimpler"></script>You also need to add Jquery (for AJAX) before ajaxsimpler. So, you can use this imports for HTML file:
<script src="https://unpkg.com/jquery"></script>
<script src="https://unpkg.com/ajaxsimpler"></script>Usage
Creating object and setup
To use Ajax Simpler, you must create object first:
var ajx = window.AjaxSimpler.create();If you are using a backend application, ex. Django, you will need to add CSRF token. For adding CSRF token, you can add:
ajx.csrftoken = "csrftoken";For example, if your app has CSRF token in cookie with different name you should use:
ajx.csrftoken = "COOKIE_NAME";Also for shorthand, you can add CSRF token when creating object:
var ajx = window.AjaxSimpler.create({ csrftoken: "csrftoken" });For adding default headers, you can use:
ajx.addDefaultHeader("Content-Type", "application/json");For deleting default header:
ajx.deleteDefaultHeader("Content-Type");Requests
For GET request you can use:
ajx.GET("URL_HERE").then((result) => console.log(result));AjaxSimpler.GET returns Promise object. Accepts, 3 parameters:
url(required) URL to request.dataData to send as url parameter.headerHeaders for request.
For POST request you can use:
ajx.POST("URL_HERE").then((result) => console.log(result));AjaxSimpler.POST returns Promise object. Accepts, 3 parameters:
url(required) URL to request.dataData to send as request body.headerHeaders for request.