Laravel 5 Ajax发布路线 [英] Laravel 5 Ajax Post route

查看:78
本文介绍了Laravel 5 Ajax发布路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试使用ajax发布而不是Get.我收到200的响应,好像它正在工作,但是控制器中的功能从未运行过.

This is my first attempt to use ajax to Post instead of Get. I get a 200 response as if it were working but the function in the controller is never being run.

我在ajax Get请求中使用了相同的概念,它们可以正常工作,但是Post无法按预期工作,并且sortable('serialize')创建了一个Post变量,因此我需要使用Post.

I used this same concept in my ajax Get requests and they work fine but the Post is not working as expected and sortable('serialize') creates a Post variable so I need to use Post.

成功警报:函数总是像成功一样运行,但是控制器功能从未被击中(我只是通过简单的数据库更改就可以验证它是否正在运行).

The alert in the success: function always runs as if it were successful but the controller function is never hit (I have it making a simple database change just to verify if it is running).

Ajax:

$(function() {
  $('[id^="sortable_"]').sortable(
    {
        connectWith: '.sortable-line-item-list-5',    
        update : function (event, ui) 
        { 
        var items = $(this).sortable('serialize');  
            $.ajax({
                type: 'post',
                url: '/api/sort_order_item',
                data: {
                    'items': items,
                },
                success: function()
                {
                    alert('looks like it is working...');
                },
            });
        }
    });
    $( '[id^="sortable_"]' ).disableSelection();
});

路线:

Route::post('api/sort_order_item', ['as' => 'api/sort_order_item', 'uses' =>'ApiController@SortOrderItem']);

控制器:

public function SortOrderItem()
{
    $this_item = \pmms\Checklist_template_line_item::findOrFail(20);
    $this_item->list_position = 1;
    $this_item->save(); 
}

推荐答案

我认为您的问题是csrf_token, 将此行放在刀片页面的头部:

I think your problem is csrf_token, Put this line in your blade page head section:

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

然后,像这样更新您的ajax代码:

then, update your ajax code like this :

$.ajax({
            type: 'post',
            url: '/api/sort_order_item',
            data: {
                'items': items,
                '_token': $('meta[name="csrf-token"]').attr('content'),
            },
            success: function()
            {
                alert('looks like it is working...');
            },
        });

让我知道它是否对您有帮助

Let me know if it helps you

这篇关于Laravel 5 Ajax发布路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆