weixin_33726318 2017-06-23 13:31 采纳率: 0%
浏览 45

Laravel AJAX 404用于路线

I am working on a Laravel 5.3 solution. I try to call a POST route via AJAX from one of my views to update a set of categories but I get a 404 error everytime I call the route.

Interesting fact: During development I was able to call the route with the JS-code shown below successfully - but since I did some updates to the controller code itself it throws a 404 but no exception.

Here is my controller action:

public function updateTree( Request $request )
{
    $data = $request->json()->all();

    $result = BlogCategory::rebuildTree($data, false);

    if($result > 0) {
        return Response::HTTP_OK;
    }
    return Response::HTTP_NOT_MODIFIED;
}

And here the JS AJAX call:

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

var updateTree = function (e) {
    var list = e.length ? e : $(e.target), output = list.data('output');

    console.log(JSON.stringify(list.nestable('serialize')));

    $.ajax({
        url: '{{ action('BlogCategoryController@updateTree') }}',
        type: "POST",
        data: JSON.stringify(list.nestable('serialize'))
    });
};

$(document).ready(function() {
    $('#nestable2').nestable({
        group: 1
    }).on('change', updateTree);
});

The controller route is bound like that in web.php

Route::post( '/service/blog/categories/updatetree', 'BlogCategoryController@updateTree' );

As you might see, I am using the Laravel NestedSet module from LazyChaser here (https://github.com/lazychaser/laravel-nestedset).

Any input is much appreciated.

Cheers, Jules

  • 写回答

1条回答 默认 最新

  • weixin_33675507 2017-06-23 13:32
    关注

    you having opening and closing quotes problem in your ajax url, use like this

    $.ajax({
        url: '{{ action("BlogCategoryController@updateTree") }}',
        type: "POST",
        data: JSON.stringify(list.nestable('serialize'))
    });
    
    评论

报告相同问题?