无法在laravel中提交表单?

时间:2019-11-18 19:58:41

标签: laravel vue.js

尝试提交表单时出现此错误

  

此路由不支持POST方法。支持的方法:GET,HEAD,PUT,PATCH,DELETE。

我一直以相同的方式执行此操作,但是这次不起作用。我不确定是否是因为url为comments/{id},我不确定这是否会影响保存内容的能力。

这是要提交的代码

baseUrl: 'comments',
customUpdateUrl: 'comments/update',

如果我将这些网址更改为

baseUrl: 'comments/' + forum.id,
customUpdateUrl: 'comments/' + forum.id + '/update',

然后我看不到该页面,该页面未加载,并且出现如下错误: 如果我离开了,那么页面会正确加载

  

data()中的错误:“ ReferenceError:未定义论坛”

     

ReferenceError:论坛未定义

     

属性或方法“论坛”未在实例上定义,但在渲染期间被引用。通过初始化属性,确保该属性在data选项中或对于基于类的组件都是反应性的。

     

渲染错误:“ TypeError:无法读取未定义的属性'theme'”

     

无法读取未定义的属性“主题”

submit() {
this.$refs.form.validate((valid) => {
    if (valid) {
        this.loading = true;
        if (!this.form.id) {
            this.$inertia.post(this.baseUrl, {
                comment: this.form.comment,
                forum_id: this.forum.id,
            }).then(
                () => {
                    this.$message({
                        type: 'success',
                        message: 'Creado correctamente.'
                    });
                    this.loading = false
                },
                (res) => {
                    this.$message.error(parseError(res)[0]);
                    this.loading = false;
                })
        } else {
            this.$inertia.post(this.customUpdateUrl + '/' + this.form.id, {
                comment: this.form.comment,
                forum_id: this.forum.id,
            }).then(
                () => {
                    this.$message({
                        type: 'success',
                        message: 'Guardado correctamente.'
                    });
                    this.loading = false
                },
                (res) => {
                    this.$message.error(parseError(res)[0]);
                    this.loading = false;
                })
            }
        } else {
            return false;
        }
        this.reset();
    });
},

这些是路线

Route::get('comments/{id}', 'ReplyController@index');
Route::resource('comments', 'ReplyController');

1 个答案:

答案 0 :(得分:0)

resources controllers中,update函数路由默认使用PUT方法。

您应该将POST请求更改为update端点的PUT请求(我向您展示了axios的解决方案,因为我真的不知道$inertia的工作原理) :

const axios = require('axios');

axios.put(this.customUpdateUrl + '/' + this.form.id, {
    comment: this.form.comment,
    forum_id: this.forum.id
})
   .then(r => {
      ...
   })