【问题标题】:What is the correct way of using axios.post in Vue 3?在 Vue 3 中使用 axios.post 的正确方法是什么?
【发布时间】:2021-11-21 17:50:24
【问题描述】:

我正在尝试让axios.postlaravel 8 作为后端在vue 3 中工作,但我得到了POST http://localhost:3000/contact 500 (Internal Server Error)

这是我的vue component(没有css):

<template>
  <section class="contact">
    <form @submit.prevent="storeContact">
      <input type="text" placeholder="Name" v-model="name">
      <input type="text" placeholder="Email" v-model="email">
      <input type="text" placeholder="Message" v-model="message">
      <button action="post" type="submit">Submit</button>
    </form>
  </section>
</template>



<script>
import {ref} from "vue";

export default {
  setup() {
    let success = false;
    let error = false;
    const name = ref(null);
    const email = ref(null);
    const message = ref(null);

    function onSuccess(msg) {
      success = true;
    };

    function onFailure(msg) {
      error = true;
    };

    function storeContact() {
      axios.post('post', {
        name: 'name',
        email: 'email',
        message: 'message'
      })
        .then((res) => {
          onSuccess(res.data.message)
        })
        .catch((error) => {
          onFailure(error.response.data.message)
        })
    };
    return {
      success,
      error,
      name,
      email,
      message,
      storeContact
    }
  }
}
</script>

另外,有没有办法像这样在axios.post 中使用array 而不是properties

axios.post('post', [name, email, message]).then(...

【问题讨论】:

  • Internal Server Error 表示服务器端错误。您的服务器可能需要特定格式的数据。你的 Laravel 日志显示了什么?

标签: javascript laravel vue.js axios vuejs3


【解决方案1】:

感谢@NikolaPavicevic 和@tony19 我能够解决它。

  1. post 字段确实是一个占位符,但我不知道它连接到route。我不得不将其更改为相应的路线/contact。这方面的错误信息写在laravel logs
  2. 确实可以将发布的信息写入(类似数组的?)对象中。此外,将onSuccessonFailure 中的代码直接放在axios 函数中可以使其更具可读性。

这是现在的代码:

<script>
import {ref} from "vue";

export default {
  setup() {
    let success = false;
    let error = false;
    const user = ref({
      name: null,
      email: null,
      message: null
    });

    function storeContact() {
      axios.post('/contact', Object.values(user.value))
        .then((res) => {
          success = true
        })
        .catch((error) => {
          error = error.data.message
        })
    };

    return {
      success,
      error,
      user,
      storeContact
    }
  }
}
</script>

非常感谢你们!我会接受@Nikola 的回答。

【讨论】:

    【解决方案2】:

    如果您需要将数组传递给 axios post,请尝试以下操作:

    <template>
      <section class="contact">
        <form>
          <input type="text" placeholder="Name" v-model="user.name">
          <input type="text" placeholder="Email" v-model="user.email">
          <input type="text" placeholder="Message" v-model="user.message">
          <button @click.prevent="storeContact">Submit</button>
          <p v-if="error">{{ error }}</p>
        </form>
      </section>
    </template>
    
    
    
    <script>
    import {ref} from "vue";
    
    export default {
      setup() {
        let success = false;
        let error = '';
        const user = ref({
          name: '',
          email: '',
          massage: ''
        });
    
        function storeContact() {
          axios.post('Your-API-URL', Object.values(user.value))
            .then((res) => {
              console.log(res.data)
              success = true
            })
            .catch((error) => {
              error = error.data.message
              )
            })
        };
        return {
          success,
          error,
          user,
          storeContact
        }
      }
    }
    </script>
    

    【讨论】:

    • 你能解释一下为什么这个改变会有帮助吗?
    • @tony19 嘿伙计,我认为axios.post('post' 是错误的来源,也许我错了,如果是这样的话,对不起,干杯:)
    • 这可能不是问题,因为 OP 看到了 500 响应。 OP 可能只是在问题中使用 'post' 作为占位符,就像您在答案中使用 'Your-API-URL' 一样。
    • @tony19 好的,就像我说的对不起,OP 发布的数据格式如何,他要求数组,也许Object.values(user) 可以解决问题?
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2021-06-29
    • 2021-01-17
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多