weixin_33691817 2019-04-14 20:26 采纳率: 0%
浏览 13

Laravel Ajax帖子调用失败

I found several solutions to this one, tried them all (csrf tokens, urls..) but none of them seems to work.

This is my setup:

html file:

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

...

    <button id="button" class="btn btn-success">Assign Selected</button>

...

My js file:

$(document).ready(function () {

 $('#button').click(function (e) {


        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
                type: "POST",
                dataType: 'json',
                url: '/wptest',
                data: { data: 'ahoj' }
            }).done(function (data) {
                console.log('Ajax was Successful!')
                console.log(data)
            }).fail(function () {
                console.log('Ajax Failed')
            });
    });

}

And my web.php file:

Route::post('/wptest','UserController@assignToWP');

In my controller there is just a simple dd($request);

Edit: network tab shows: 200 OK

Problem: I always get Ajax Failed after button click. What do I miss?

  • 写回答

1条回答 默认 最新

  • 七度&光 2019-04-14 20:38
    关注

    Ok I found the solution:

    The whole problem is, that in order for ajax to be successful, the controller needs to pass and return. dd function kills the controller before return and ajax fail therefore.

    Make sure controller always gets to its return line.

    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存