簡體   English   中英

Ajax Laravel返回視圖500錯誤

[英]Ajax Laravel return view 500 Error

我將幻燈片中的每個滑塊值(Jquery Ui Slider)通過Ajax傳遞給我的Controller。

Slider + Ajax看起來像這樣:

    $("#sliderNumCh").slider({
        range: "min",
        min: 0,
        max: 20,
        step: 1,
        value: numbersOfChapters,
        change : function(e, slider){
            $('#sliderAppendNumCh').empty();
            var sliderValue  = slider.value;
            var getSliderVal = document.getElementById('sliderValue').value = sliderValue;
            var getPrId      = document.getElementById('editId').value;

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: 'post',
                url: "{{ Route('editProductPost', $product->id) }}",
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
                data: {
                    value: getSliderVal,
                    productId : getPrId
                },
                success: function (option) {
                    console.log(getSliderVal);
                }
            });
        },
    });

因此,我確實有這個想法:

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

我的路線看起來像這樣:

Route::post('edit/{productID}', ['as' => 'editProductPost', 'uses' => 'ProductController@editProductPost']);

在我的Controller方法中,我稱之為:

public function editProductPost(Request $request)
{
    $sliderValue = $request->get('value');
    Log::info($sliderValue);

    return view('productRom.edit', [
        'sliderValue' => $sliderValue
    ]);
}

Log :: info($ sliderValue)告訴我,我確實在每張幻燈片上獲得了正確的滑塊值。

當我嘗試返回編輯視圖時,我在控制台中收到此錯誤:

POST http:// localhost / myApp / public / product / edit / 73 500(內部服務器錯誤)

我怎么解決這個問題?

編輯

由於我在我的觀點中有這一行:

<form action="{{ route($route) }}"...>

路由變量沒有定義,所以我在return語句中添加了這個。

return view('productRom.edit', [
    'sliderValue' => $sliderValue,
    'route' => 'editProductPost'
]);

錯誤消失但當我嘗試訪問$sliderValue變量時,如下所示:

<p>{{ isset($sliderValue) ? $sliderValue : "nothing" }}</p>

nothing打印出來

編輯

控制器:

    public function editProductPost(Request $request)
    {

        return response()->json([
            'sliderValue' => $request->get('value')
        ]);
   }

查看(阿賈克斯):

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            type: 'post',
            url: "{{ Route('editProductPost', $product->id) }}",
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            },
            data: {
                value: getSliderVal,
                productId : getPrId
            },
            success: function (response) {
                // check it has some data
                if (response.length) {
                    // spit it out
                    console.log(response);
                } else {
                    console.log('nothing returned');
                }
            }
        });

您的控制器方法應如下所示:

public function editProductPost(Request $request)
{
    return response()->json([
        'sliderValue' => $request->get('value')
    ]);
}

您不應該將視圖返回到ajax請求。

你的ajax成功方法應如下所示(例如):

// first arg is the data returned by the controller method
success: function (response) {
    console.log(response);
}

它應該輸出這樣的東西:

{
    'sliderValue': 4
}

您的路線名為editProductPost,但您正在搜索editProduct

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM