Im encodes the auction website in Laravel 5.0, which simulates real-time updates using an AJAX poll that runs every 5 seconds. The problem is that my server returns the sporadic status of HTTP 401.
My route is constructed as follows:
Route::post(auction/live/update, ' AuctionController@ajaxSendUpdate ');
My controller looks like this:
public function ajaxSendUpdate() { // Business logic: queries database, couple of Ifs, etcโฆ $data = array('success' => true, 'otherStuff' => $myData); return Response::json($data); }
Finally, my controller is configured as follows:
// 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);
This code works fine about 95% times. However, it can break with two different results:
1) The server responds to error 401 after a while and is never restored. In this case, we need to log in again. After logging in, everything goes well, and this result will never be repeated.
2) The server responds with sporadic 401, but is restored in the next (or after several) polling requests.
Im uses Laravel 5.0 and the updated version of Xampp on Windows. The error is easily reproduced using WAMP on Windows. Not tested on Linux and OSX. I read this and this and sorted the topics on laracasts.com and other forums, but I can not solve the problem ...