Ajax Fetch with Authentication

by Ben Clayton

JavaScript

// https://zinoui.com/blog/ajax-basic-authentication

var myHeaders = new Headers();
myHeaders.append("Authorization", 'Basic ' + btoa('myuser:mypswd'));
fetch("https://www.example.org/protected-data.php", {
    credentials: "include",
    headers: myHeaders
}).then(function (response) {
    return response.json();
}).then(function (json) {
    console.log(json);
});