首页 > 解决方案 > 如何在我的 JS 代码中使用 flask.Response?

问题描述

我已经尝试了一段时间,但我未能设法从我的后端、我的前端获得返回值。

这是我的代码:

def add_New_Form():
    resp = Response("something") #Note that the response value is a string
    return resp

而且我希望能够在我的前端使用它(我正在使用 Vue)。我不能 jsonify resp,因为 Response 不是 jsonifyable。我试过了:

var name = this.$http.post(*My request here*)

但这并不像我期望的那样工作。

提前致谢。

标签: javascriptpythonjsonflaskvue.js

解决方案


你永远不会处理响应。当您使用this.$httpwhich时,vue-resource您的请求可能会返回一个Promise,然后您可以从中链接:

this.$http.post(*My request here*)
  .then( response => {
      console.log(response.data) // it's your string
  })

推荐阅读