【发布时间】:2021-05-04 03:57:47
【问题描述】:
我在 python 中编写了一个 API,并在 http://127.0.0.1:5000 上运行它。 (使用 Flask。另外,我目前正在开发服务器上运行它以进行测试)
但是当我尝试在 Flutter 中使用 http 包获取响应时,我得到了一个 XMLHttpRequest 错误。
void api() async {
const String url = "http://127.0.0.1:5000/dosomething?query=This doesn't work!"; # this is an example
Response response = await get(url);
print(response.body); # I'm printing this for testing
}
注意:我也检查了 URL,它有效。
可能是什么问题?以及如何解决?
重要提示 - 当我尝试使用在网络上运行的 API(在本例中为 WorldTimeAPI)时,以下答案有效。但是,当我尝试使用我的代码时,它以某种方式不起作用。虽然当我手动测试它时它工作得很好。
这是我的 API 代码的简化版本 -
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/dosomething', methods = ['GET'])
def API():
result = {}
result['result'] = user_input = str(request.args['query'])
return jsonify(result)
【问题讨论】:
-
用
Future替换void()然后试试
标签: python api flutter localhost