Javascript Fetch API Tutorial - Post Data
https://mindsit.net/call-a-rest-web-service-in-javascript-using-fetch-api
by Xavier Langlat
HTML
<button onclick="callWs()">
Click
</button>
<br/>
<b>Data to Post :</b>
<div>
{ title: 'foo', body: 'bar', userId: 1}
</div>
<br/>
<b>Response Status :</b>
<div id="result">
</div>
JavaScript
callWs = function(){
// The Endpoint URL
let url = 'https://jsonplaceholder.typicode.com/posts';
// Data to Post
let data = {
title: 'foo',
body: 'bar',
userId: 1
};
let headers = {
'Content-type': 'application/json; charset=UTF-8'
}
// Request options
let options = {
method: 'POST',
body: JSON.stringify(data),
header: headers
};
fetch(url, options)
.then(function(response) {
// Render the Response Status
document.getElementById('result').innerHTML = response.status;
})
}