简体   繁体   English

Laravel AJAX 404用于路线

[英]Laravel AJAX 404 for route

I am working on a Laravel 5.3 solution. 我正在研究Laravel 5.3解决方案。 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. 我尝试从一个视图通过AJAX调用POST路由以更新一组类别,但是每次调用路由时都会收到404错误。

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. 有趣的事实:在开发过程中,我能够成功使用下面显示的JS代码调用路由-但由于我对控制器代码本身进行了一些更新,因此它抛出404,但没有异常。

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: 这里是JS AJAX调用:

$.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 控制器路由与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 ). 如您所见,我在这里使用LazyChaser的Laravel NestedSet模块( https://github.com/lazychaser/laravel-nestedset )。

Any input is much appreciated. 非常感谢任何输入。

Cheers, Jules 干杯,朱尔斯

you having opening and closing quotes problem in your ajax url, use like this 您在ajax网址中存在引号和结尾引号问题,请像这样使用

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

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

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