首页 > 解决方案 > 我在使用“AXIOS PATCH”方法时遇到问题

问题描述

我正在尝试使用“axios patch”方法更新我的数据库记录。

这是我的代码:

editClient(client) {
  let data = new FormData();
  data.append("name", this.client.name);
  data.append("email", this.client.email);
  data.append("phone", this.client.phone);
  data.append("_method", "PATCH");
  axios
    .post(`/api/clients/${client.id}`, data)
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));
},

我试过这段代码:

axios
    .patch(`/api/clients/${client.id}`, {
      name: this.client.name,
      phone: this.client.phone,
      email: this.client.email
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

但它也不起作用。

我得到的错误是

POST http://localhost:8000/api/clients/27 500(内部服务器错误)

{message: "SQLSTATE[42S22]: Column not found: 1054 Unknown co…4 02:12:57"` = updated_at:"2019-05-24 02:12:57"})", exception: "Illuminate\Database\QueryException", file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php", line: 664, trace: Array(60)}
exception: "Illuminate\Database\QueryException"
file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line: 664
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"2011"' in 'where clause'

当我尝试这段代码时:

axios
    .patch(`/api/clients/${client.id}`, {
      data: this.client
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

我得到的错误是

app.js:285 PATCH http://localhost:8000/api/clients/27 422(无法处理的实体)

{message: "The given data was invalid.", errors: {…}}
errors:
email: ["The email field is required."]
name: ["The name field is required."]
phone: ["The phone field is required."]
__proto__: Object
message: "The given data was invalid."

我是 axios 和 vue 的新手。我正在尝试学习如何使用 axios 构建和 CRUD api。

我试图找到其他方法,但我找不到任何方法。

这是我的控制器:

public function update(Client $client) {
        $val = $client ? ','.$client : '';
        $client->update($this->_val($val));
        return back();
    }

    public function _val($val) {
        return request()->validate([
            'name' => ['required', 'min:2', 'unique:clients,name'.$val],
            'email' => ['required', 'email', 'unique:clients,email'.$val],
            'phone' => ['required', 'alpha_num'],
        ]);
    }

标签: laravelvue.jsaxios

解决方案


您的 axios 邮政编码很好。它工作正常。您在 post 字段中得到的错误是larvel error column not found.

我猜您在保存数据时在某些列名中输入了拼写错误,或者您错误地输入了未在表中的未知列。

请提供 laravel 侧代码,以便我们可以看到您在哪里遇到错误。顺便说一下你的问题=>你axios.post code是正确的


推荐阅读