Halo guys,, welcome back to my channel.. kali ini kita bahas tentang ajax client sebagai request API kita.. kita akan coba gunakan library baru dan yang simple tentunya guna untuk kebutuhan website kita.. cekidot..
A simple ajax client with jQuery like ajax API for javascript
oke pertama kalian bisa buka repos diatas dan bisa lihat beberapa contoh cara penggunaannya.. berikut salah satu penggunannya :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax-client example</title>
</head>
<body>
<script src="https://raw.githubusercontent.com/riversun/ajax-client/master/dist/ajaxclient.js"></script>
<script>
const ajax = new AjaxClient2();
//Data object to send
const data = {
message: "hello"
}
//Do async post request
ajax.postAsync({
url: 'http://localhost:9999/api',//Endpoint
headers: {
'X-Original-Header1': 'header-value-1',//Additional Headers
'X-Original-Header2': 'header-value-2',
},
contentType: 'application/json; charset = UTF-8',//content-type of sending data
data: JSON.stringify(data),//text data
dataType: 'json',//data type to parse when receiving response from server
timeoutMillis: 5000,//timeout milli-seconds
success: response => {
console.log(response);
},
error: e => {
console.error('Error occurred');
},
timeout: e => {
console.error('Timeout occurred.');
}
});
</script>
</body>
</html>
oke cukup sekian cyaaaa….