簡體   English   中英

Ajax請求未提供任何數據

[英]Ajax request is not giving any data

我正在學習一個教程,並使用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請求填充的categorys對象,但是category boject未定義

假設/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