i want to get the http response from the php web page and save that in my variable in javascript code
this is my code :
let state="";
function ajax(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(this.responseText);
};
xhr.onerror = reject;
xhr.open('GET', url);
xhr.send();
});
}
ajax("./code.php")
.then(function(result) {
this.state=result;
console.log(this.state)// work correctly
})
.catch(function() {
});
console.log(this.state) //undefinde
when i use console log in my save function, state variable logged correctly but outside the function console logged undefiend
how can i fix this problem
i'm beginner in javascript , I apologize for that :(