【问题标题】:Can't post with vue.js无法使用 vue.js 发布
【发布时间】:2020-12-26 14:23:32
【问题描述】:

我想使用“vue-resource”发布表单数据,但出现错误。 我用 vue-cli 和 vuetify 编写了代码。

错误

[Vue 警告]:v-on 处理程序中的错误:“TypeError: this.$http.post(...).error is not a function”

App.vue

<template>
  <v-app>
    <v-main>
      <v-container class="px-0">
        <v-form ref="form" @submit="addEvent">
            <p class="text-uppercase">Question?</p>
              <v-radio-group v-model="newEvent.select1" row>
                <v-radio label="aaa" value="aaa"></v-radio>
                <v-radio label="bbb" value="bbb"></v-radio>
              </v-radio-group>
            <input type="submit" depressed color="primary" value="OK!">
        </v-form>
      </v-container>
    </v-main>
  </v-app>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      users: [],
      newEvent: {
        select1: '',
      }
    }
  },
  methods: {
    addEvent: function (e) {
    e.preventDefault()
        if (this.isValid) {
          console.log("success")
          console.log("select1: " + this.newEvent.select1)

          this.$http.post('URL..', this.newEvent, function (data, status, request) {
            console.log("post success")

            console.log(status)
            }).error(function (data, status, request) {
              console.log("post failed")
            })
        }else{
          console.log("need edit")
        }
      },
  },
    computed: {
      isValid: function () {
        var valid = true
        for (var key in this.data) {
          if (!this.data[key]) {
            valid = false
          }
        }
        return valid
      }
    },
};
</script>
 

我尝试在main.js中写“import VueResource from'vue-resource'”,但是没有效果。

我该如何解决?

【问题讨论】:

    标签: javascript vue.js vuetify.js vue-resource


    【解决方案1】:

    尝试使用axiosfetch

    // install axios first
    
    import axios from 'axios'
    
    // ... some code here
    
    axios.post(url, data).then(response => {}).catch(error => {})
    
    
    // or you can use fetch
    
    let response = await fetch(url, { method:'POST', headers: {'Content-Type': 'application/json;charset=utf-8'}, body: JSON.stringify(data) })
    
    // getting result
    let json = await response.json()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多