我正在使用flask发送一个图像,如果我使用postman,我会得到“200OK”,并且我也可以看到图像,但是当我使用react native获取图像并将其显示在屏幕上时,我得到了"error Unexpected�in JSON at position 0“。
代码如下:
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()中写什么,我已经尝试了几个我在互联网上找到的解决方案,但都没有奏效。
下面是flask代码:
@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')发布于 2019-07-03 18:35:08
您将发送一个图像作为响应,并接受application/json作为响应。
你应该这样做:
var image
fetch("http://10.0.2.2:5000/api/echo", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
x: 0,
y: 0
})
})
.then(response => response.blob())
.then(images => {
// Then create a local URL for that image and print it
image = URL.createObjectURL(images)
console.log(image)
}) .catch(error => {
console.error(error);
});
}; https://stackoverflow.com/questions/56867734
复制相似问题