open-index/open-npm huggingface dataset:
npm package: @sunpodder/ajax.js
Description: A **XMLHTTPRequest** wraper for the browser. Works with both, promises and callback function.
Author: SunPodder
Latest version: 2.0.0

# 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.
<br><br>


## Load Ajax.js with script tag
```html
<script src="https://sunpodder.github.com/ajax.js/dist/ajax.min.js" ></script>
```

## Or use ES6 import
```javascript
npm i @sunpodder/ajax.js
```
```javascript
import Ajax from "@sunpodder/ajax.js"
```

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

```

## POST Request
```javascript
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.

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

For **GET** requests
```javascript
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.