【发布时间】:2020-01-13 10:05:10
【问题描述】:
我正在尝试将一些 json 数据从烧瓶服务器显示到我的 html 页面,但我有一个 TypeError: NetworkError when attempting to fetch resource. 和一个 Promise { <state>: "rejected" }。
server.py
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/hello', methods=['GET'])
def hello():
jsonResp = {'jack': 4098, 'sape': 4139}
print(jsonify(jsonResp))
return jsonify(jsonResp)
if __name__ == '__main__':
app.run(host='localhost', port=8989)
script.js
function getHello() {
const url = 'http://localhost:8989/hello'
const response = fetch(url)
console.log(response);
document.getElementById("demo").innerHTML = response;
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="getHello()">click</button>
<label id="demo"></label>
<script src="script.js"></script>
</body>
</html>
当我点击按钮时,标签部分中还有一个[object Promise]。
我做了最简单的代码,但它不起作用。
【问题讨论】:
-
试试这个 stackoverflow.com/questions/57857647/… fetch 是一个可能有问题的新 api。我使用的代码绝对有效,应该至少返回一些东西。
-
我肯定在这里用你的代码做错了我仍然有一个网络错误
NetworkError: A network error occurred.但没有更多的承诺。我也用 axios 测试过同样的问题
标签: javascript python api flask fetch