【问题标题】:axios post for multiple data with flask and vuejsaxios 使用烧瓶和 vuejs 发布多个数据
【发布时间】:2018-09-10 03:46:33
【问题描述】:

我想做的就是从我的网络用户输入中获取context_answerstreatment_answers 并将其带到Flask 上。 (我对此很陌生,对不起,我对自己在做什么含糊不清)

`context_answers = {"a":[1], "b":[2], "c":[3], "d":[4]}
treatment_answers = {"y":[10]}`    

我能够通过以下方式获得 context_answers:

`methods: {
      handleSubmit() {    
        axios.post("/submit_survey", this.context_answers)
      }
    }`

在烧瓶上

`@app.route("/submit_survey", methods=["POST"])
def submit_survey():
    context = request.get_json(force=True)
    context_df = pd.DataFrame.from_dict(context)`

但是如何在同一个axios post方法中得到this.treatments_answers呢?在submit_survey?

我想创建一个包含以下内容的数据框:

a b c d y 1 2 3 4 10

非常感谢!

【问题讨论】:

  • 不清楚你在问什么。也许展示每条数据的示例(即context_answerstreatments_answers)以及您希望收到的数据在您的 Flask 应用程序中的样子

标签: flask vue.js axios


【解决方案1】:

如果你想过去很多参数,你可以这样做:

methods: {
  handleSubmit() {    
    axios.post("/submit_survey", {context_answers: this.context_answers, 
                                                         treatments_answers: this.treatments_answers})
  .then( 
     (response) => { console.log(response) },
     (error) => { console.log(error) }
   )
  }
}

或者试试这个:

 methods: {
      handleSubmit() {    
        axios.post("/submit_survey", {context_answers: this.context_answers, 
                                                             treatments_answers: this.treatments_answers})
      .then(response => { 
         console.log(response)
      })
      .catch(error => {
         console.log(error)
      });
    }

【讨论】:

  • 谢谢大卫。那我以后怎么申请呢?我知道这是超级基本的。
  • 我的意思是当我只有一个 context_answer 时,我使用 context = request.get_json(force=True) 将其提取出来并将其转换为 pandas 数据框。那么现在我该如何挽救治疗呢?
  • 理论上您应该像这样在变量中捕获轴子:const response = await axios.post( ... 然后制作 responseconsole.log 并告诉我你所看到的
  • Promise {<pending>} __proto__ : Promise catch : ƒ catch() arguments : (...)
  • 我编辑了我的答案,看看handleSubmit 方法是怎样的,再次制作一个console.log 响应并告诉我你看到了什么
猜你喜欢
  • 2021-08-11
  • 2014-07-16
  • 2017-11-07
  • 2013-04-13
  • 1970-01-01
  • 2014-03-21
  • 2013-02-01
  • 1970-01-01
  • 2018-06-29
相关资源
最近更新 更多