繁体   English   中英

尝试将数据从 vue js 发布到 Laravel 后端时请求中止

[英]Request aborted when trying to post data from vue js to laravel backend

我一整天都在为一个问题而苦苦挣扎。 每次我想从我的 vue js 组件向我的 laravel 后端发布数据时,它都会说请求已中止。 我真的看不出我的代码有什么问题。 任何帮助是极大的赞赏。 我安装了 CORS,所以这不是问题。 我试图清除路由缓存,但也没有用。

控制器存储方法。 这是我检查标题是否存在的地方,如果不存在,则继续将故事保存在数据库中:

public function store(
    AuthenticatedUserId $authenticatedUserId,
    CreateNewRecordInDatabase $createNewRecordInDatabase,
    FindDataById $findDataById, 
    RetrieveAllData $retrieveAllData,
    Request $request, 
    $writer
    )
{
    $writer = $findDataById->findDataById(User::class, $writer);

    $stories = $retrieveAllData->retrieveAllData(Story::class);

    foreach ($stories as $check_title) {
        if ($check_title->title == $request->title) {
            return redirect()
                ->route('writer.create', ['writer' => $authenticatedUserId->authId()])
                ->with('title_exists', 'Title already exists');
        }
    }

    $createNewRecordInDatabase->createRecord($writer->stories(), [
        'title' => $request->title,
        'story' => $request->story,
        'published_on' => date('m-d-Y'),
        'category_id' => $request->category_id
    ]);

    return response()->json('success');
}

API路由:

Route::post('/dashboard/writer/{writer}/store', 'Story\StoryController@store');

Vue 组件:

<template>
<div class="row">
    <form class="col s12">
        <input type="hidden" name="_token" :value="csrf">

        <div class="row">
            <div class="col s12">
                <input  v-model="title" type="text" 
                        class="input-field" 
                        placeholder="Give it a catchy title" 
                        required
                        >
                <label for="title">Give it a catchy title</label>
            </div>
        </div>

        <div class="row">
            <div class="input-field col s12">
                <select v-model="category_id" name="category_id">
                     <option 
                        v-for="category in categories"
                        v-bind:key="category.id"
                        v-bind:value="category.id"
                        >{{ category.category }}
                     </option>
                </select>
            </div>
        </div>

        <div class="white col s12" id="editorjs"></div>

        <div class="col s12">
            <button @click="publishStory" class="btn waves-effect waves-light">Publish</button>
        </div>
    </form>

</div>

<script>

import EditorJS from '@editorjs/editorjs';
import Header from '@editorjs/header';
import Link from '@editorjs/link';
import List from '@editorjs/list';
import Quote from '@editorjs/quote';

export default {
    props: ['categories', 'writer'],
    data() {
        return  {
            title: null,
            category_id: null,
            story: [],
            editor: new EditorJS({
                tools: {
                    header: {
                        class: Header,
                        inlineToolbar: true
                    }, 
                    list: {
                        class: List,
                        inlineToolbar: true
                    }, 
                    link: {
                        class: Link,
                        inlineToolbar: true
                    },
                    quote: {
                        class: Quote,
                        inlineToolbar: true
                    }
                }
            }),
            csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content')
        }
    },
    methods: {
        publishStory() {
            this.editor.save()
            .then(outputData => this.story = outputData.blocks)
            .catch(err => console.log(err))
            axios.post(`http://localhost:8000/api/dashboard/writer/${this.writer}/store`, {
                    title: this.title,  
                    story: this.story,
                    category_id: this.category_id,
                })
                .then(res => console.log(res))
                .catch(err => console.log(err))
        }

    },
    mounted() {}
}
</script>

尝试增加php.ini设置

max_input_time 
upload_max_filesize
post_max_size   
memory_limit
max_input_vars

PHP 截断 POST 数据

暂无
暂无

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

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