【发布时间】:2019-11-14 00:04:11
【问题描述】:
我正在使用烧瓶发送图像以响应本机,如果我使用邮递员并且我也看到图像,我会得到“200 OK”,但是当我使用本机反应来获取图像并将其显示在屏幕上时我在 JSON 中的位置 0 处收到“错误 Unexpected token �”。
代码如下:
fetch("http://10.0.2.2:5000/api/echo", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
x: 0,
y: 0
})
})
.then(response => response.text())
.then(responseJson => {
console.log(JSON.parse(responseJson));
})
.catch(error => {
console.error(error);
});
};
我不知道我应该在 .then() 中具体写什么,我尝试了几个在互联网上找到的解决方案,但都没有奏效。
这是烧瓶代码:
@app.route('/api/echo', methods=['GET', 'POST', 'DELETE', 'PUT'])
def add():
while(True):
data = request.get_json()
img = plt.imread('mapflask.png')
plt.figure()
plt.imshow(img)
plt.scatter(100, 20, s=30, c='red', marker='o')
plt.scatter(30, 40, s=30, c='blue', marker='o')
plt.axis('off')
plt.savefig("test100.png", transparent=True,
bbox_inches='tight', dpi=150)
filename = 'test100.png'
return send_file(filename, mimetype='image/jpg')
【问题讨论】:
标签: javascript react-native flask fetch-api