简体   繁体   English

将$ .post路由到控制器的方法

[英]routing $.post to controller's method

New to laravel, working on a premade project was replicatinga a functionality to another section but now I'm stuck with a: laravel的新手,从事预制项目是将功能复制到另一部分,但现在我陷入了:

Failed to load resource: the server responded with a status of 404 (Not Found) (17:26:57:137 | error, network)   at http://localhost/testimonials/sort

The URL of where the action is made: http://localhost/ *****/public/index.php/admin/testimonials 进行操作的URL: http:// localhost / ***** / public / index.php / admin / testimonials

I have this piece of javascript that gets called by an event: 我有一段通过事件调用的javascript:

sortTestimonials: function () {
        $("#sortable_1, #sortable_2").sortable({connectWith: ".connectedSortable", stop: function () {
                var t = [];
                t = $(".connectedSortable").sortable("toArray"), 
                $.post("/testimonials/sort", {list: t})
            }})
    },

That $.post(..) supposedly goes to my routes.php where I have the routes of the controller/section 那$ .post(..)应该去我的routes.php,在那里我有控制器/部分的路由

# Testimonials Routes
    Route::resource('testimonials', 'AdminTestimonialsController', ['except' => ['show', 'destroy']]);
    Route::post('testimonials/toggleVisibility', 'AdminTestimonialsController@toggleVisibility');
    Route::post('testimonials/delete', 'AdminTestimonialsController@delete');
    Route::post('testimonials/sort', 'AdminTestimonialsController@sort');
        //this last one being the one it's supposed to grab

And then I have my controller AdminTestimonialsController.php where I can't get to (just showing the method in question obviously) 然后我有我无法访问的控制器AdminTestimonialsController.php(显然只是显示了所讨论的方法)

public function sort() {
        $order = Input::get('list');
        if (Request::ajax()) {
                        $result = $this->testimonial->sortArticles($order);
        }
        return Redirect::route('admin.testimonials.index');
    }

So what am I doing wrong here or what am I missing for it to work? 那么,我在这里做错什么了吗?或者我想让它正常工作是什么?

As your application doesn't live in the root of your domain you can't just use /testimonials/sort . 由于您的应用程序不在域的根目录中,因此您不能仅使用/testimonials/sort It will make a request to http://localhost/testimonials/sort instead of http://localhost/*****/public/index.php/testimonials/sort . 它将向http://localhost/testimonials/sort发出请求,而不是http://localhost/*****/public/index.php/testimonials/sort发出请求。

You can define a baseUrl js variable somewhere in your PHP / Blade template and use that when you make ajax requests. 您可以在PHP / Blade模板中的某个位置定义baseUrl js变量,并在发出ajax请求时使用它。

<script>
    var baseUrl = "{{ URL::to('/') }}";
</script>

And then later : 然后

$.post(baseUrl + "/testimonials/sort", {list: t})

Note that you still need the / in front of the URL because the baseUrl will be without trailing slash. 请注意 ,您仍然需要在URL前面加上/ ,因为baseUrl将不带斜杠。

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

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