This is the sync function.I call it inside an Ajax, but how is synchronous it´s deprecated. ¿Is there any way to run it as if it were asynchronous? Or to make it asynchronous? Thanks.
function fetchHeader(url, hdr) {
try {
var req=new XMLHttpRequest();
req.open("HEAD", url, false);
req.send(null);
if(req.status== 200){
return req.getResponseHeader(hdr);
}
else return false;
} catch(er) {
return er.message;
}
I've tried that, but it says: response is not defined and callback is not a function.
function fetchHeader(url, hdr, callback) {
try {
var req=new XMLHttpRequest();
req.onreadystatechange = function() {
if(this.status == 200) {
callback(req.getResponseHeader(hdr), req);
}else{
console.log("callback is not called");
}
}
req.open("HEAD", url, true);
req.send(null);
} catch(er) {
return er.message;
}
}
And I call it:
fetchHeader(dirGen+json[i],'Last-Modified', function(header, request) {
if (response.statusCode == 200) {
return header;
console.log(header);
}else {
console.log("Failed answer");
}
});
response.statusCodebut you do not have aresponsevariable so that's why you get the error about responseresponsewould not exist until the callback gets called. Why is not it called?