【问题标题】:jquery post call to a route laravel not workingjquery post call to a route laravel 不工作
【发布时间】:2017-10-10 12:45:36
【问题描述】:

我正在尝试从 JS 文件到 laravel 路由发布 jquery 帖子,但似乎它不起作用,我不知道为什么。

我的主要目标是:从选中了复选框的表中获取所有 ID,并在 SQL 上更改它们的列值。

所以,这是我的 JS 函数:

    function concludeAll() {
        var arrayIds = [];

        $('.checkbox1').each(function() {
            var $this = $(this);
            var $id = $this.attr('id');

            if ($this.is(":checked")) {
                arrayIds.push($id);
            }
        });

        var json = {
            "ids": arrayIds
        };

        $.post('http://localhost:8000/controle/pending/concludeAll',
            {
                '_token': $('meta[name=csrf-token]').attr('content'),
                ids: arrayIds
            })
            .error(

             )
            .success(

             );     
}

这是我的路线:

Route::group(['prefix' => '/controle'], function() {
Route::post('/pending/concludeAll/', function() {

    $input = Input::only('ids'); 
    $input = $input['ids']; 

    foreach($input as $id) {
        $student = new App\Aluno();
        $student = $student->where('id', '=', $id)->first();
        $student->pending = '0';
        $student->save();
    }

}); };

因此,如果我检查表格上的几行并点击调用该函数的按钮,我的控制台上不会发生任何事情。在网络 > 标头 > 表单数据上,我看到了令牌和 ID,如下所示:

_token:fNWunwF8yDLSycrkBE684wgQcyK9dP8wbR7VgLjC 身份证[]:23 ids[]:20

在预览时,我看到的页面完全相同。 在响应时,我看到了页面的 HTML。

我也试过 dd($input);在路线上,但没有什么不同..

尝试了 php artisan route:clear 并没有发生任何不同。

如果我更改http://localhost:8000/controle/pending/concludeAll2 的url 名称,则不会返回任何错误,这让我抓狂...

任何想法如何使这个帖子调用路线?谢谢!

【问题讨论】:

  • 把你的arrayIds转换成JSONids: JSON.stringify(arrayIds)然后看看...
  • 如果你的控制台什么也没发生,你怎么能看到那些标题?
  • @HimanshuUpadhyay 它的唯一区别是:在网络 > 标题 > 表单数据我看到 ids:["23","20","29"] 而不是 ids[]:23 ids[ ]:20。我认为这是必要的,但还没有解决问题。不过还是谢谢。
  • @Devon 在控制台上什么都没有,但在 Chrome 上我去网络 > 我看到一个标题选项卡。
  • 我认为你应该从 url 中删除 controle:像这样 $.post('http://localhost:8000/pending/concludeAll'

标签: javascript php jquery laravel post


【解决方案1】:

尝试改变路线

Route::post('/controle/pending/concludeAll', function() {
    $input = Input::only('ids'); 
    $input = $input['ids']; 
    foreach($input as $id) {
        $student = new App\Aluno();
        $student = $student->where('id', '=', $id)->first();
        $student->pending = '0';
        $student->save();
    }

});

在JS函数中:

    function concludeAll() {
        var arrayIds = [];

        $('.checkbox1').each(function() {
            var $this = $(this);
            var $id = $this.attr('id');

            if ($this.is(":checked")) {
                arrayIds.push($id);
            }
        });
       arrayIds=JSON.stringify(arrayIds);

        var json = {
            "ids": arrayIds
        };

        $.post('http://localhost:8000/controle/pending/concludeAll',
            {
                '_token': $('meta[name=csrf-token]').attr('content'),
                ids: arrayIds
            })
            .error(

             )
            .success(

             );     
    }

【讨论】:

  • 很抱歉,我忘记发布上面的路线在 Route::group('controle') 内。
  • @GabrielAugusto 能否请您张贴表格,可能会提供一些线索。发布方法的问题也可能导致类似的问题
  • 对不起,这里没有表格。这是一个我选择项目的表格,当我点击一个普通按钮时,它会调用具有 post 方法的 javascript 函数。
猜你喜欢
  • 2015-09-10
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2021-02-04
  • 2022-11-20
  • 2017-07-30
  • 2015-11-21
  • 1970-01-01
相关资源
最近更新 更多