I made XMLHttpRequest
get a json value from a server using this code:
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let msg = JSON.parse(xhr.responseText);
// Send the request to our POST Servlet
}
};
xhr.open("GET", "/libs/granite/csrf/token.json", true);
xhr.send();
However, I want to use fetch
in my code, however, upon trying the code below, I was instantly answered with a 403 error:
fetch("/libs/granite/csrf/token.json", { method: "GET" })
.then((response: any) => {
if (response.status === 200 || response.statusText === "OK") {
callback(ResponseCode.SUCCESS);
} else {
callback(ResponseCode.ERROR);
}
})
.catch((error: any) => {
callback(ResponseCode.ERROR);
});
I want to know the difference between the two and what should I modify to make the fetch
work.