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'))
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题