PUT
request to the passed URL.XMLHttpRequest
web api to make a PUT
request to the given url
.HTTP
request header with setRequestHeader
method.onload
event, by running the provided callback
function.onerror
event, by running the provided err
function.err
to log the request to the console's error stream by default.const httpPut = (url, data, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open('PUT', url, true);
request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
request.onload = () => callback(request);
request.onerror = () => err(request);
request.send(data);
};
const password = 'fooBaz'; const data = JSON.stringify({ id: 1, title: 'foo', body: 'bar', userId: 1 }); httpPut('https://jsonplaceholder.typicode.com/posts/1', data, request => { console.log(request.responseText); }); /* Logs: { id: 1, title: 'foo', body: 'bar', userId: 1 } */
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️