1

我正在尝试获取使用 ajax.ajax() 发出的 api 请求的响应,并将响应存储到 HTML5 本地存储中的 ['apiResponse'] 中(但其余的 python 函数处理而不等待它被放置到本地存储)。

因此,我需要在收到响应之前等待它,并且我认为我可以执行下面的操作,让程序在继续之前等待。

不幸的是,每次我放一个while循环时,浏览器似乎都会冻结......

如果有人知道如何让 Brython 和浏览器停止冻结或其他方法来做我想做的事......

(这真的对我有帮助,因为这是成功获得 Spotify api 请求响应之前的唯一步骤)


from browser import ajax #to make requests
from browser.local_storage import storage as localStorage #to access HTML5 Local Storage

import json #to convert a json-like string into a Python Dict

#Request to the API
def on_complete(req):
    if req.status==200 or req.status==0:
        localStorage['apiResponse'] = req.text
    else:
        print("An error occured while asking Spotify for data")

def apiRequest(requestUrl, requestMethod):
    req = ajax.ajax()
    req.bind('complete', on_complete)
    req.open(requestMethod, requestUrl, True)
    req.set_header('Authorization', localStorage['header'])
    req.send()

def response():
    while localStorage['apiResponse'] == '':
        continue
    print('done')
    return json.loads(localStorage['apiResponse'])

提前致谢!

4

0 回答 0