我把Python数组转储到
json.dumps
的响应中,现在我正试图以Javascript列表的形式检索数据。
@app.route('/get_scales')
@cross_origin()
def get_scales():
classes = inspect.getmembers(sys.modules['mingus.core.scales'], inspect.isclass)
scales = [class_[0] for class_ in classes if ('Error' not in class_[0] and class_[0] != '_Scale')]
return json.dumps(scales)
getScales() {
// create a new XMLHttpRequest
var xhr = new XMLHttpRequest();
// get a callback when the server responds
xhr.addEventListener("load", () => {
// update the state of the component with the result here
console.log(xhr.responseText);
// open the request with the verb and the url
xhr.open("GET", "http://127.0.0.1:5000/get_scales");
// send the request
xhr.send();
var formatted_response = JSON.stringify(xhr.responseText);
console.log(JSON.parse(xhr.responseText));
return xhr.responseText;
当我把getScales中的函数记录到xhr.responseText的控制台类型时,它显示的是String,但当试图用JSON.parse来解析它时,它抛出一个错误。试着先把它字符串化,像上面那样,也没有帮助。