fetch try js

coba2 call api with fetch

by oktaviardi pratama

HTML

<div id="banner-message">
  <p>Hello World</p>
  <button>Change color</button>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}

JavaScript

// find elements
var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("click", () => {
let endpoint_url = 'https://id8.shortlyst.ai/tenant-integration/sme-demo'
let token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjQ0MDY3NjUsImp0aSI6IjJGNkE0NDBPa3hPVUtaalJOeU5vdkY5NU5yZSIsImlzcyI6InRlc3QzIiwic3ViIjoiMVhOSmp0dFV0aE1nTkNaYTV6OXdqZjRIblNtIiwidXNlcl9pZCI6IjFYTkpqdHRVdGhNZ05DWmE1ejl3amY0SG5TbSIsImVtYWlsIjoic21lLWRlbW9Ac2hvcnRseXN0LmFpIiwic2NvcGUiOlsiY2FuZGlkYXRlLnJlYWQiLCJqb2IucmVhZCIsImpvYi53cml0ZSIsIm1lbWJlci5yZWFkIiwibWVtYmVyLndyaXRlIiwicHJvZmlsZS1zZXR0aW5nLnJlYWQiLCJwcm9maWxlLnJlYWQiLCJwdWJsaWMtY2FuZGlkYXRlLnJlYWQiLCJyZWFjaG91dC5yZWFkIiwicmVhY2hvdXQud3JpdGUiXSwiZmlyc3RfbmFtZSI6IlNtZSIsImxhc3RfbmFtZSI6IkRlbW8iLCJhdmF0YXIiOiIiLCJ0ZW5hbnQiOiJzbWUtZGVtbyIsInJvbGUiOiJ0ZW5hbnQtYWRtaW4tc21lIiwiaXNfY2xpZW50IjpmYWxzZX0.P3fk6VVzuanPdaNU_THsWrmJ2BYTIF72XPzJhHgL1pc'


   fetch(endpoint_url, {
        method: 'GET',
        // mode: 'no-cors',
        withCredentials: true,
        credentials: 'include',
        // cache: 'no-cache',
        headers: {
          'Content-Type': 'application/json',
          // 'Accept': 'application/json',
          'Authorization': token,
          'Access-Control-Allow-Origin': '*',
          // 'Access-Control-Allow-Methods': '*',
          'Access-Control-Allow-Headers': 'Origin, X-Requested-With,Authorization,  Content-Type, Accept'
        }
        // headers: [
        //   ['Content-Type', 'application/x-www-form-urlencoded'],
        //   ['Content-Type', 'multipart/form-data'],
        //   ['Content-Type', 'text/plain'],
        //   ['Authorization', token],
        // ]
      }).then(responseJson => {
        console.log(responseJson)
      })
        .catch(error => {
          console.log(error)
        })
})