@sunpodder/ajax.js

2.0.0 • Public • Published

Ajax.js

A XMLHTTPRequest wraper for the browser. Works with both, promises and callback function.

Callback functions are removed in version 2.0. Use promises instead.



Load Ajax.js with script tag

<script src="https://sunpodder.github.com/ajax.js/dist/ajax.min.js" ></script>

Or use ES6 import

npm i @sunpodder/ajax.js
import Ajax from "@sunpodder/ajax.js"

GET Request

//An Ajax object is now available to the window
Ajax.get("https://google.com")
.then(response => document.write(response))

POST Request

Ajax.post(
  "https://yourpostapi.com/api",
  //pass your request parameters as an object
  {
    name: "Ajax",
    post: true,
    get: false
  })
.then(data => document.write(data))

You can also set custom headers and data types to meet your needs.

Ajax.js automatically detects if you have passed additional headers or type or just your query object, it works according to it.

Just slightly modify the data object with properties queries, headers and type. If you don't want to use these just avoid these as above.

Ajax.post(
  "https://yourpostapi.com",
  {
    queries: {
      name: "Sun",
      age: 17
    },
    headers: {
      JWT: "just a random string"
    },
    type: "json"
  }
).then(...)

For GET requests

Ajax.get(
  "https://google.com/search",
  //you can pass a data object here
  //same as the post requests
  //to pass queries here instead of hardcode the url
   {
    queries: {
      name: "Sun",
      age: 17
    },
    headers: {
      JWT: "just a random string"
    }
  }
).then(...)

NOTE: Ajax.js automatically parses your response if it's a valid JSON or else it will just return it as plain text.

Package Sidebar

Install

npm i @sunpodder/ajax.js

Weekly Downloads

1

Version

2.0.0

License

WTFPL

Unpacked Size

14.4 kB

Total Files

13

Last publish

Collaborators

  • sunpodder