如何访问 Javascript 文件中 Flask 路由的 return 值?
How can I access the return value of a Flask route within a Javascript file?
我遇到的问题是在 flask 路由 (resp["login"]) 中访问 return 语句中的值。我试图在 javascript 文件中使用此值并将其用作查询参数。但是每当我在 javascript 文件中尝试 console.log() 时,我都会得到一个 promise 对象。但是我找不到在哪里可以找到 Flask 应用程序的价值。我以为它会在下面的响应对象中,但没有这样的运气。
@app.route('/route', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def handle_callback():
if request.method == 'POST':
payload = {
blahhh
}
headers = {'Accept': 'application/json', 'Access-Control-Allow-Origin': '*'}
req = requests.post(token_url, params=payload, headers=headers)
# make another request after this using access token with updated header field
resp = req.json()
if 'access_token' in resp:
oauthHeader = "token " + resp['blahhh']
headers = {'Authorization': oauthHeader}
access_token_url = 'https://blahhh.com'
r = requests.get(url=access_token_url, headers=headers)
resp = r.json()
return resp["login"]
else:
return "error", 404
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const codeGit = urlParams.get('code')
const sub = {codeGit};
const res = fetch('http://localhost:4000/route', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'code': codeGit
},
credentials: 'include'
}).then(response => {
if(response.status == 200){
console.log('Success! ' + response.json() )
}
}).catch(error => {
console.log('error with access token req!')
})
console.log(res)
response.json()
return一个承诺
Return value: A Promise that resolves to a JavaScript object. This object could be anything that can be represented by JSON — an object, an array, a string, a number...
.then(response => {
if(response.status == 200){
return response.json();
} else {
// handle this somehow
}
}).then(json => {
console.log('Success! ' + JSON.stringify(json))
}).catch(error => {
console.log('error with access token req!')
})
我遇到的问题是在 flask 路由 (resp["login"]) 中访问 return 语句中的值。我试图在 javascript 文件中使用此值并将其用作查询参数。但是每当我在 javascript 文件中尝试 console.log() 时,我都会得到一个 promise 对象。但是我找不到在哪里可以找到 Flask 应用程序的价值。我以为它会在下面的响应对象中,但没有这样的运气。
@app.route('/route', methods=['GET', 'POST'])
@cross_origin(supports_credentials=True)
def handle_callback():
if request.method == 'POST':
payload = {
blahhh
}
headers = {'Accept': 'application/json', 'Access-Control-Allow-Origin': '*'}
req = requests.post(token_url, params=payload, headers=headers)
# make another request after this using access token with updated header field
resp = req.json()
if 'access_token' in resp:
oauthHeader = "token " + resp['blahhh']
headers = {'Authorization': oauthHeader}
access_token_url = 'https://blahhh.com'
r = requests.get(url=access_token_url, headers=headers)
resp = r.json()
return resp["login"]
else:
return "error", 404
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const codeGit = urlParams.get('code')
const sub = {codeGit};
const res = fetch('http://localhost:4000/route', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'code': codeGit
},
credentials: 'include'
}).then(response => {
if(response.status == 200){
console.log('Success! ' + response.json() )
}
}).catch(error => {
console.log('error with access token req!')
})
console.log(res)
response.json()
return一个承诺
Return value: A Promise that resolves to a JavaScript object. This object could be anything that can be represented by JSON — an object, an array, a string, a number...
.then(response => {
if(response.status == 200){
return response.json();
} else {
// handle this somehow
}
}).then(json => {
console.log('Success! ' + JSON.stringify(json))
}).catch(error => {
console.log('error with access token req!')
})