1

我正在创建一个爱好应用程序,其中 Angular 是前端,Python 充当后端。Angular 中的一个组件正在向 Python 发送 HTTP GET,python 用 jsonpickled 对象响应。

我希望解开(解码)在 HTTP RESPONSE 中收到的 jsonpickled 对象。 unpickle(解码)将发生在接收 HTTP REPSONSE 对象的 Angular 的 component.ts 文件中。

按照此处示例文件中的说明进行操作,但它不起作用:https ://github.com/cuthbertLab/jsonpickleJS (在测试目录下有 testUnpickle.html 文件,我曾用作参考,但脚本标签中的部分只是没有' t 执行)。

下面是 Angular 的 component.ts 文件中的代码:

 ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.url = 'http://127.0.0.1:5000/api/restaurants/' + this.id
    this.http.get(this.url).subscribe(
      response => 
      {
        this.name = response['name'];
        this.location = response['location'];
        this.photos = response['photos']; // this is the jsonpickled object, I want to unpickle it so as to get a File Object.
        console.log(response)
      }
    )
  }

下面是来自 component.html 文件的片段:

<body>
  <script type="text/javascript" src="../../assets/jsonpickleJS-master/build/jsonpickle.min.js"
  data-main= "jsonpickleJS/main"></script>
  <script type="text/javascript">
    picfileobj = jsonpickle.decode(photos)
    console.log("getting decoded file for pic")
    console.log(picfileobj)
  </script>
</body>
4

1 回答 1

0

能够使用下面的方法解码在 HTTP RESPONSE 中收到的 jsonpickle 对象。

  1. 从https://github.com/cuthbertLab/jsonpickleJS下载 jsonpickle js 文件
  2. 导入 angular.json (jsonpickle.min.js)
  3. 在 xyz.component.ts 文件中声明 var jsonpickle:
  4. 使用 jsonpickle.decode(jsonpickled_object) 方法获取原始文件对象。
于 2020-10-22T10:42:07.570 回答