Laravel和AJAX轮询器中偶尔出现401错误 [英] Laravel and AJAX sporadic 401 errors in a poller

查看:263
本文介绍了Laravel和AJAX轮询器中偶尔出现401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Laravel 5.0编写一个拍卖网站,该网站使用每5秒执行一次的AJAX轮询器来模拟实时更新.问题是我的服务器返回了零星的HTTP 401状态.

I’m coding an auction website in Laravel 5.0 that simulates realtime updates by using an AJAX poller that is executed every 5 seconds. The problem is that my server returns sporadic HTTP 401 status.

我的路线是这样构建的:

My route is build like this:

Route::post(auction/live/update, 'AuctionController@ajaxSendUpdate');

我的控制器是这样的:

public function ajaxSendUpdate() {
    // Business logic: queries database, couple of Ifs, etc…
    $data = array('success' => true, 'otherStuff' => $myData);
    return Response::json($data);
}

最后,我的轮询器的设置如下:

Finally my poller is setup like this:

// a bit of HTML
function getAuctionUpdate() {
    setTimeout(function () {
    $.ajax({
        type: "POST",
        url: "{!! url('auction/live/update')!!}",
            dataType: 'json',
            data: {
                auctionID: $('#auctionID').val()
            },
            success: function (data) {
                if (data['success']) {
                    // Updates some labels, etc.
                   getAuctionUpdate(); // Rearms itself
                }
            }
    } }); // Not sure if all brackets are correct in this snippet but they are 100% on real code
}, 5000);

此代码可以正常运行约95%的时间.但是,它可能会导致2种​​不同的结果:

This code runs fine about 95% of times. However it can break with 2 different outcomes:

1)服务器在一段时间后响应错误401,并且永不恢复.在这种情况下,我们需要再次登录.登录后,一切顺利,此结果再也不会发生.

1) Server responds error 401 after some time and never recovers. In this scenario we need to login again. After login, everything goes well and this outcome never occurs again.

2)服务器以零星的401响应,但在下一个(或几次之后)轮询请求中恢复.

2) Server responds with sporadic 401 but recovers in the next (or after a few) polling requests.

我正在使用Laravel 5.0和Windows上的Xampp的最新版本.在Windows上使用WAMP可以轻松重现该错误.未经Linux或OSX的测试.我已阅读

I’m using Laravel 5.0 and an up-to-date version of Xampp on Windows. The error is easily reproduced with WAMP on Windows. Not tested in Linux nor OSX. I've read this and this and assorted threads in laracasts.com and other forums but I am unable to solve the problem...

推荐答案

经过数小时的测试,我相信我已经解决了这个问题,即使我不完全了解如何并且即使这是一个通用的答案也可以应用于类似的解决方案案例.

After many hours of testing I believe I solved this issue even if I do not fully understand how and even if this is a universal answer that can be applied to similar cases.

在开发初期,我在kernel.php中禁用了VerifyCsrfToken中间件,因此我的AJAX请求没有发送任何_token.启用VerifyCsrfToken中间件并立即发送_token会使所有HTTP 401错误消失.现在,我开始遇到另一个问题:更加零星的HTTP 500错误.快速浏览一下日志,发现所有HTTP 500错误都是由TokenMismatchException引起的.

Early in development I had the VerifyCsrfToken middleware disabled in kernel.php so I was not sending any _token with my AJAX requests. Enabling VerifyCsrfToken middleware and sending the _token immediately made all HTTP 401 errors disappear. Now, I started to get a different issue: even more sporadic HTTP 500 errors. A quick glance at the logs showed that all HTTP 500 errors were caused by TokenMismatchException.

然后我遇到了.按照网页上的说明,我将其放在master.page标头中:

I then came across this. Following the webpage instructions I put this in my master.page header:

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

这在我的master.page javascript中:

And this in my master.page javascript:

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

现在某种程度上一切都很好.因此,出于所有目的和目的,我原来的问题已解决,但我仍然不明白:

And somehow everything is fine now. So, for all intents and purposes, my original problem is solved but I still cannot understand:

1 –如果在kernel.php中禁用了VerifyCsrfToken中间件,为什么没有通过AJAX请求发送任何_token时出现零星的HTTP 401错误?

1 – Why was I getting sporadic HTTP 401 errors when I was not sending any _token with my AJAX requests if I had VerifyCsrfToken middleware disabled in kernel.php?

2 –如果我开始通过我的AJAX请求发送_token,为什么在kernel.php中启用VerifyCsrfToken中间件时为什么会偶尔出现TokenMismatchException?

2 – Why did I started to get sporadic TokenMismatchException when I enabled VerifyCsrfToken middleware in kernel.php if I started to send the _token with my AJAX requests?

3 –为什么X-CSRF-TOKEN最终解决了HTTP 500错误问题?请记住,所有错误都是偶发性的,而且不是永久性的:我冒着风险地说,所有AJAX请求中的95%到98%都可以,但是只有很少的任何问题.

3 – Why did X-CSRF-TOKEN finally solved the HTTP 500 error issue? Bear in mind that all errors were sporadic and not permanent: I would risk saying that 95 to 98% of all AJAX requests went fine, only a small number of them had any issue whatsoever.

这篇关于Laravel和AJAX轮询器中偶尔出现401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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