我正在遵循一个教程,并使用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>
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请求填充到category对象,但类别boject未定义
答案 0 :(得分:0)
假设/api/category
用于您的index()
方法,未得到任何结果的原因是因为您没有返回以data
为键的任何内容({{ 1}}),您需要的信息将在res.data.data
中。
res.data