【问题标题】:Cannot post it always say the value is empty不能发布它总是说值是空的
【发布时间】:2021-01-08 06:14:37
【问题描述】:

我似乎不知道如何进行 POST 或连接到后端 (Laravel 8)。我正在研究 Quasar 和 Laravel。 我有两个单独的文件夹,一个在后端,一个在前端。

我分别运行它们(左:Laravel,右:Quasar)

我已经在 quasar 代理中创建了 devServer 来获取后端。

 devServer: {
  https: false,
  port: 8080,
  open: true,
  proxy: {
    // proxy all requests starting with /api to jsonplaceholder
    "/api": {
      target: "http://localhost:8080/", //tried this one also http://127.0.0.1:8000
      changeOrigin: true,
      pathRewrite: {
        "^/api": ""
      }
    }
  }}

这是我的 Vue 文件。

      <!-- Dialog  -->
  <q-dialog
    v-model="addProductForm"
    transition-show="slide-up"
    transition-hide="slide-down"
  >
    <q-card style="width: 700px; max-width: 80vw;">
      <q-card-section>
        <div class="text-h6">Add Category</div>
      </q-card-section>
      <div class="q-pa-md">
        <q-input outlined v-model="categoryName" label="Category name" />

        <div class="q-ma-md float-right">
          <q-btn label="Cancel" />
          <q-btn
            class="q-ml-md "
            label="Create"
            color="primary"
            @click="createCategory"
          />
        </div>
      </div>
    </q-card>
  </q-dialog>
  <!-- Dialog (END) -->
</q-card></q-page>
<script>
import axios from "axios";

export default {
  data() {
    return {
      addProductForm: true,
      categoryName: "Shoes",
      categories: []
    };
  },
  methods: {
    createCategory() {
      this.$axios
        .post("http://127.0.0.1:8000/api/createCategory", this.categoryName)
        .then(response => {
          // this.categories.unshift(response.data);
          console.log(response.data);
        })
        .catch(() => {
          this.$q.notify({
            color: "negative",
            position: "top",
            message: "Unable to save category name",
            icon: "report_problem"
          });
        });
    }
  }
  // mounted() {
  //   console.log(this.categories);
  // }
};
</script>

每次我发帖。它总是说 categoryName 是空的。

这是我的 api.php

我的类别控制器。

谁能知道如何进行这项工作?它确实说 categoryName 是空的。

【问题讨论】:

  • 把这个.post("http://127.0.0.1:8000/api/createCategory", this.categoryName)改成.post("http://127.0.0.1:8000/api/createCategory", { categoryName: this.categoryName})

标签: laravel vue.js quasar-framework


【解决方案1】:

是axios post数据格式的问题。

axios 发布请求应该包含如下数据部分

{categoryName: this.categoryName}

【讨论】:

  • 如何为“127.0.0.1:8000?”创建环境变量,这样我就不必再次输入127.0.0.1:8000
  • 在您的前端项目中添加 .env 文件。并且只要在代码前面需要使用 process.env.your_variable_name
猜你喜欢
  • 1970-01-01
  • 2019-08-02
  • 2012-03-20
  • 2022-11-03
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
相关资源
最近更新 更多