简体   繁体   English

Ajax请求未提供任何数据

[英]Ajax request is not giving any data

I am following a tutorial and making a single page app using laravel and vue.js. 我正在学习一个教程,并使用laravel和vue.js制作一个单页应用程序。 But at a certain point, an ajax request is not giving me the expected output. 但是在某个时候,ajax请求没有给我期望的输出。 I have been trying many ways but nothing is working. 我已经尝试了许多方法,但是没有任何效果。

template I'm facing problem with 我面临的模板

<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>

Category controller where I'm sending axios request 我发送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);
    }
}

I expect to get the categories object to be populated with the axios request, but the categories boject is undefined 我希望获得要用axios请求填充的categorys对象,但是category boject未定义

Assuming that /api/category is for your index() method, the reason you're not getting any results is because you're not returning anything keyed by data (the 2nd in res.data.data ), the information you need will be in res.data . 假设/api/category是用于index()方法的,则没有得到任何结果的原因是因为您没有返回任何以data为键的datares.data.data的第二res.data.data ),因此您需要的信息将在res.data

created() {
    axios.get("/api/category")
        .then(res => this.categories = res.data)
        .catch(err => console.log(err)); 
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM