Angularjs 代码在 Chrome 中不起作用

Angularjs code not working in Chrome

我使用 Angularjs 和 Laravel 构建了我的应用程序。我使用 Firefox 进行开发和测试,所以在引起我注意之前我没有注意到这一点。我的应用程序只能在 Firefox 中运行,但不能在基于 Chrome 或 Chrome 的浏览器中运行,我也无法追踪问题的根源。

我有一个按钮指令,当点击它时执行一个函数 post 数据到服务器,现在这在 Firefox 中工作正常但在 Chrome 中,你会认为这样的问题跨浏览器兼容性不起作用与客户端代码有关,但我的应用程序抛出 500 内部服务器错误 ONLY in Chrome or Chrome-based浏览器。

我通过 post 在内部发出警报来测试该函数,该函数确实被调用了。然后我去了服务器端,找到了处理请求的控制器,删除了分配函数中的代码,只是做了一个

return "hello";

还是不行,一直报同样的错误。接下来,我没有使用我的按钮指令,而是使用了一个普通的 html 按钮,我又一次遇到了同样的错误。我不知道发生了什么或如何解决它。

这是我在控制器中的功能:

$scope.event.addToCart = function(event, no_of_tickets, index) {
    $scope.selectedIndex = index;
    $scope.state = 'working';

    var req = {
        method: 'POST',
        url: '/buyEventTicket',
        headers: {
            'X-XSRF-Token': $("meta[name='csrf_token']").attr("content")
        },
        data: {
            event: event,
            no_of_tickets: no_of_tickets
        }
    }

    $http(req)
        .success(function (data, status, headers, config) {

            $rootScope.$broadcast('TICKET_ADDED', true);

            $scope.selectedIndex = -1;
            $scope.state = 'ready';
        })
        .error(function (data, status, headers, config) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            //alert(data);

        });

}

之前,我查看了服务器抛出的错误是这样的:

DecryptException in Encrypter.php line 142: Invalid data.

注意:由于我无法准确指出错误的原因,因此我不知道我的代码的哪一部分与此相关。

将 "X-XSRF-Token" 更改为 "X-CSRF-TOKEN"。注意 "XSRF" 和 "CSRF".

之间的区别

来自documentation:"Note: The difference between the X-CSRF-TOKEN and X-XSRF-TOKEN is that the first uses a plain text value and the latter uses an encrypted value, because cookies in Laravel are always encrypted. If you use the csrf_token() function to supply the token value, you probably want to use the X-CSRF-TOKEN header."

此外,文档指出加密的 XSRF 令牌存储在 XSRF-TOKEN cookie 中,并且 Angular 已经为您获取了该值。根据 Angular 的 documentation:"When performing XHR requests, the $http service reads a token from a cookie (by default, XSRF-TOKEN) and sets it as an HTTP header (X-XSRF-TOKEN)." 因此,您似乎可以完全删除手动设置令牌的尝试,框架应该为您处理。