【发布时间】:2019-09-11 10:23:06
【问题描述】:
我正在学习一个教程并使用 laravel 和 vue.js 制作一个单页应用程序。但在某个时候,ajax 请求并没有给我预期的输出。我一直在尝试很多方法,但没有任何效果。
我遇到问题的模板
<template>
<v-container>
<v-form @submit.prevent="create">
<v-autocomplete
:items="categories"
item-text="name"
item-value="id"
v-model="form.category_id"
label="Category"
></v-autocomplete>
</template>
<script>
export default {
data() {
return {
form: {
title: null,
category_id: null,
body: null
},
categories: {}, //Expecting to populate the object with axios request.
errors: {}
};
},
created() {
axios.get("/api/category").then(res => (this.categories = res.data.data)); //This line is not populating the 'categories' object.
},
};
</script>
我发送 axios 请求的类别控制器
class CategoryController extends Controller
{
public function index()
{
return Category::latest()->get();
}
public function store(Request $request)
{
$category = new Category();
$category->name = $request->name;
$category->slug = Str::slug($request->name);
$category->save();
return response('Created',Response::HTTP_CREATED);
}
public function show(Category $category)
{
return $category;
}
public function update(Request $request, Category $category)
{
$category->update(['name'=>$request->name,'slug'=>Str::slug($request->name)]);
}
public function destroy(Category $category)
{
$category->delete();
return response(null,Response::HTTP_NO_CONTENT);
}
}
我希望使用 axios 请求填充类别对象,但类别对象未定义
【问题讨论】:
-
没有改变,结果相同
-
您的控制器中的哪个方法适用于
"/api/category"? -
@YeasirArafatHridoy 控制台中的任何错误。检查网络(Chrome 检查)选项卡并查看您的请求响应。
-
@RossWilson index() 方法用于“/api/category”
-
在这种情况下,请在下面查看我的答案。
标签: javascript ajax laravel vue.js vuetify.js