I can’t disclose the URL and what API I’m using because it breaches my work’s policy but hopefully I can provide enough information for assistance. I have a javascript function that runs on a on-click (I’m eventually building up a form that will make use of this fetch).
function getdata() {
let customer_po = document.getElementById("customer").value;
let access_token;
let data = {
'grant_type': 'REDACTED'
}
let state = {
"state": "REDACTED"
}
fetch(prod_auth, {
method: "POST",
headers: login_header,
body: new URLSearchParams(data),
})
.then(response => response.json())
.then(result => console.log(result))
}
This works and I can access the JSON dictionary in the console. However, when I try to use my access_token variable declared earlier in such a way:
.then(response => response.json())
.then(result => {access_token = result["access_token"];})
and console.log(access_token) gives undefined. I need to access this variable in my getdata() function so it can be used as a parameter in another fetch call. Any help would be appreciated. Thank you!