首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在react native中从flask api获取镜像

如何在react native中从flask api获取镜像
EN

Stack Overflow用户
提问于 2019-07-03 18:10:02
回答 1查看 910关注 0票数 1

我正在使用flask发送一个图像,如果我使用postman,我会得到“200OK”,并且我也可以看到图像,但是当我使用react native获取图像并将其显示在屏幕上时,我得到了"error Unexpected�in JSON at position 0“。

代码如下:

代码语言:javascript
运行
复制
  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代码:

代码语言:javascript
运行
复制
@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')
EN

回答 1

Stack Overflow用户

发布于 2019-07-03 18:35:08

您将发送一个图像作为响应,并接受application/json作为响应。

你应该这样做:

代码语言:javascript
运行
复制
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);
  });
}; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56867734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档