【问题标题】:How to update product records with Vue, Axios and Laravel如何使用 Vue、Axios 和 Laravel 更新产品记录
【发布时间】:2020-10-10 11:48:33
【问题描述】:

当我检查数据库中的响应时,我的 Vue 使用 axios put 方法从 ant-design 发送了一个表单,值为 null 我尝试了几次尝试传递正确的参数,但均未成功。传入参数的正确语法是什么。

这是我在 Vue 中的表单

<a-form :form="formEdit">
<a-input type="text" class="form-control" :value="show1prod.product_name" @input="show1prod.product_name = $event.target.value" />
<a-textarea :value="show1prod.product_description" @input="show1prod.description_name = $event.target.value" :rows="5" />
<button type="button" html-type="submit" @click="update(show1prod.id)" class="btn btn-default">Save</button>
</a-form>

这是 axios put 配置

import axios from 'axios'
const Api = axios.create({
  baseURL: 'http://localhost:8000/api',
  withCredentials: true
})

Api.defaults.withCredentials = true

export default Api
export default {
    
    update (id,product_name, product_description) {
        return Api.put('/products/update/' + id,product_name, product_description)
    }
    
}

这个脚本我没有包含整个导出默认基本这是它里面的内容

data(){
 return{
  formEdit:  {
    product_name: '',
    product_description: ''
  }
 }
},
methods(){
 update (id) {
       let product_name = this.show1prod.product_name;
       let product_description = this.show1prod.product_description;
          Products.update(id, product_name, product_description)
          .then((response) => {
            //this.product_name = response.data
            alert('success')
          })
          .catch((error) => {
            alert(error)
          })
        }
}

这是我的 Laravel 路由和控制器代码

Route::put('/products/update/{id}', 'ProductsController@update');
public function update(Products $request, $id)
    {
        $id = Products::find($id);
        
        request()->validate([
            'product_name' => 'required',
            'product_description' => 'required'
        ]);
        $id->product_name = $request->product_name;
        $id->product_description = $request->product_description;
        $id->save();
        return response()->json($id);
    }

我缺少什么以及如何解决这个问题?

【问题讨论】:

  • 您在 Laravel 中的表单验证正在寻找 product_nameproduct_description,但您永远不会将这些值发送到服务器。您的验证失败。 Laravel 会根据 headers 自动确定最佳响应类型,在这种情况下它是 ajax,因此它会将带有 422 错误代码和错误消息的错误包发回。
  • 真的吗?我认为这段代码“this.formEdit.validateFields((err, values)”正在处理我的表单值,谢谢,我马上试试
  • 我做了更改,但仍然有错误,我应该在方法的参数中包含什么,因为“表单”和“数据”不起作用

标签: laravel vue.js axios


【解决方案1】:

您没有通过 put 请求发送数据,一个简单的示例如下所示,

<template>
  <form @submit.prevent="submit">
      <input v-model="formEdit.product_name">
      <textarea v-model="formEdit.product_description">
  </form>
</template>

<script>
import axios from 'axios'

export default {
    data() {
        return {
            formEdit: {
                product_name: "",
                product_description: ""
            }
        }
    },

    methods: {
        submit(){
            axios.put('/product/update', this.formEdit)
        }
    },
}
</script>

【讨论】:

  • 不应该这些数据已经在我声明的表单属性中可用,所以我想我不必再次重新声明它们,但我仍然遵循这个但仍然没有工作
  • 在Ajax请求中,表单数据不会被发送,你必须告诉Axios你要发送什么数据,你没有以非常典型的方式设置你的表单,通常你会使用v -model 将表单输入绑定到 vue 数据对象。在您的请求中发送该数据。
  • 我在 input 和 textarea 中添加了 v-model="product_name" 并在里面添加了数据 formEdit: { product_name etc.. } 但它抛出的错误是在渲染期间没有定义但引用
  • 我用一个更完整的例子更新了我的答案,我省略了 ant-design、预填充表单、axios baseurl 设置和 put 请求的实体 ID,但这是非常基本的如何发送数据
  • 非常感谢@squareborg,我将复制它并在我的代码中实现,顺便说一下,输入具有来自数据库的初始值,这也可能是发生此类错误的原因之一
猜你喜欢
  • 2020-04-14
  • 1970-01-01
  • 2019-06-12
  • 2021-06-23
  • 2021-01-11
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 2022-06-19
相关资源
最近更新 更多