久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5:Ajax Post 500(內部服務器錯誤)

Laravel 5: Ajax Post 500 (Internal Server Error)(Laravel 5:Ajax Post 500(內部服務器錯誤))
本文介紹了Laravel 5:Ajax Post 500(內部服務器錯誤)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試通過 ajax 向數據庫提交數據.提交文章頁面在沒有 ajax 的情況下工作正常.我添加了 console.log() 只是為了看看是否有任何事情發生,但我收到了這個錯誤:

I'm trying to submit data to the database via ajax. The submit article page works fine without ajax. I've added console.log() just to see if anything is going through, but instead I'm getting this error:

POST http://localhost/laravel-5/public/articles/create500(內部服務器錯誤)

POST http://localhost/laravel-5/public/articles/create 500 (Internal Server Error)

我的代碼有什么問題?是 javascript 還是控制器?

What's wrong with my code? Is it the javascript or the controller?

我在 laravel.log

C:xampphtdocslaravel-5vendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php:53 中的異常 'IlluminateSessionTokenMismatchException'

exception 'IlluminateSessionTokenMismatchException' in C:xampphtdocslaravel-5vendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php:53

路線

Route::resource('articles', 'ArticlesController');

控制器

public function store(RequestsArticleRequest $request)
    {

        $article = new Article($request->all());
        Auth::user()->articles()->save($article);

        $response = array(
            'status' => 'success',
            'msg' => 'Article has been posted.',
        );
        return Response::json($response);
    }

jQuery

$(document).ready(function() {
    $('#frm').on('submit', function (e) {
        e.preventDefault();
        var title = $('#title').val();
        var body = $('#body').val();
        var published_at = $('#published_at').val();
        $.ajax({
            type: "POST",
            url: 'http://localhost/laravel-5/public/articles/create',
            dataType: 'JSON',
            data: {title: title, body: body, published_at: published_at},
            success: function( data ) {
                $("#ajaxResponse").append(data.msg);
                console.log(data);
            }
        });
    });

查看

<link rel="stylesheet" >

<h1>Write a New Article</h1>

<hr>

{!! Form::open(['url' => 'articles', 'id' => 'frm']) !!}
    <p>
        {!! Form::label('title', 'Title:') !!}
        {!! Form::text('title') !!}
    </p>

    <p>
        {!! Form::label('body', 'Body:') !!}
        {!! Form::textarea('body') !!}
    </p>

    <p>
        {!! Form::label('published_at', 'Date:') !!}
        {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
    </p>

    <p>
        {!! Form::submit('Submit Article', ['id' => 'submit']) !!}
    </p>
{!! Form::close() !!}

<h3 id="ajaxResponse"></h3>

@if($errors->any())
    <ul>
    @foreach($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@endif

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="{{ URL::asset('assets/js/ArticleCreate.js') }}"></script>

});

推薦答案

當你通過 POST 向資源控制器發出請求時,它會自動調用 store 方法:

When you make a request via POST to a resource controller, it automatically calls store method:

Verb    Path        Action  Route Name
----------------------------------
POST    /articles   store   articles.store

所以,你只需要把ajax url改成:

So, you just need to change ajax url to:

$.ajax({
        type: "POST",
        url: 'http://localhost/laravel-5/public/articles',

當您需要發送會話令牌時,您可以添加一個全局元標記,例如您的網站:

When you need to send the session token, you can add a global meta-tag like this is you website:

<meta name="csrf-token" content="{{ csrf_token() }}">

然后,只需通過ajax的標題添加令牌:

Then, just add the token via ajax's headers:

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

如果您使用 Form::open() 函數 (LaravelCollective),它會添加一個隱藏輸入,其中包含名稱為 _token 的令牌作為值.因此,您可以刪除元標記并像這樣編輯 ajax 的標題:

If you are using Form::open() function (LaravelCollective) it adds a hidden input with the token as value with the name _token. So, you can remove the meta-tag and edit your ajax's headers like this:

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('[name="_token"]').val()
        }
});

這篇關于Laravel 5:Ajax Post 500(內部服務器錯誤)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 美女黄视频网站 | 69精品久久久久久 | 国产精品久久久av | 国产亚洲精品久久情网 | 鲁大师一区影视 | 日韩在线播放第一页 | 久久福利电影 | 久久久久久久久久久久久久国产 | av中文字幕在线观看 | 亚洲天堂影院 | 免费观看一级特黄欧美大片 | 国产成人在线免费 | 亚洲a一区二区 | 日韩欧美在线一区 | 亚洲精品在线视频 | 日屁网站| av黄色在线 | 福利视频日韩 | 亚洲精品国产精品国自产在线 | 深夜福利影院 | 国产精品色 | 一级片在线观看 | 免费在线观看91 | 美国a级毛片免费视频 | 国产免费一区二区三区免费视频 | 一区二区三区中文字幕 | 成人精品免费视频 | 99精品欧美 | 五月激情综合 | 在线一区| 日韩高清国产一区在线 | 美国十次成人欧美色导视频 | 欧美一区二区免费电影 | 国产美女高潮 | 国产亚洲精品一区二区三区 | 欧美成人精品一区二区男人看 | 波多野结衣二区 | 色站综合 | 永久免费av | 欧美日韩综合 | 久久久免费少妇高潮毛片 |