Laravel 4 生产:ajax 调用更新时出现 403 禁止错误

Laravel 4 on production: 403 forbidden error on ajax call to update

我有一个 laravel 4 应用程序,只需使用 laravel 进行常规保存即可正常运行。 "Regular save" 意味着我没有使用 ajax 调用 URL 来保存表单中的数据,而是使用 laravel 到 URL /revocable/users/page/1/update。

我在 ajax 调用 url 时收到 403 禁止错误: /revocable/users/page/1/update

这里失败了:

$.ajax({
    type: action,
    cache: false,
    dataType: 'json',
    url: '/revocable/users/page/1/update',
    data: {
        'oSerializeArray':  [array]
    },
    beforeSend: function() {
    }
})
    .done( function( data, text, jqxhr ) {
        data.success;
        //data.iPersonsPK;
        window.location.replace(sUrlEdit);
    })
    .fail( function ( data, jqxhr ) {
        data.success;
    })
    .always( function ( data ) {
        data.success;
    })

return false;

我认为这是我的 apache httpd.conf 的虚拟主机设置。当我收到 403 错误时,我没有虚拟主机文件。

我尝试用这个创建一个虚拟主机文件:

<Directory "/home/myultrat/public_html/secure/test/*">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny 
Allow from all
</Directory>

ErrorLog /usr/local/apache/domlogs/secure.myultratrust.com.errors

但问题是一样的。我读到这可能是跨站点脚本错误。但它在同一个域中。所有这些代码在我使用 WAMP 的本地机器上运行良好。所以我认为这是虚拟主机配置问题。

哦,我不得不提一下 laravel 应用程序在子目录中,而不是 "traditional" 放在子域根目录中的方式。

如何解决这个问题?

非常感谢和祝福<><, 维克多

已更新

如此简单的修复。我需要更新亲戚 URL.

将提交给​​ $.ajax 的 URL 从 '/revocable/users/page/1/update' 更改为 '/test/revocable/users/page/1/update' 解决了这个问题。 @prabhakaran8737 在 URL 路径上部分正确是不正确的。但是(正如我在对他的回答的评论中提到的),一直以来,我认为 URL 是相对于发出请求的位置(即 '/test/revocable/users/page/1/update')但是 $.ajax 看到URL 调用基于它的根域。所以我需要修改 URL 以基于根并相应地更改它(即 '/test/revocable/users/page/1/update')。

我也试过使用绝对 URLs 但这似乎也没有用。所以来自根的亲戚 URL 就是答案。相对 URL 必须有开头的 / 正斜杠。我试过 ./,我也试过不使用正斜杠,但这些都不起作用。

[编辑] 请注意,我想我是在 Prabhakaran 回复之前发布的。但这仍然使我认为某人或我自己可以更清楚地解决这个问题。我祈祷这会很快得到解决。 :)

这不是答案,但可能有助于找到答案。 :)

我理解到一定程度了。当我单击 "save" 按钮时,表单将数据保存在 POST 请求中。这个POSTURL是 http://secure.myultratrust.com/test/revocable/users/page/3/update

Laravel 路由有效并保存数据。

但是,当我通过执行 $.ajax 调用的 "save an continue" 按钮保存数据时,但仍然是相同的 POST URL Apache 突然抱怨找不到 404 error 页面。

相同的文件和 Laravel 应用程序适用于我使用 2.4 的 WAMP。

我的 WAMP 本地服务器中有这个:

<VirtualHost *:80>
    ServerName l4-confide
    ServerAlias l4-confide
    DocumentRoot "C:\wamp\www\test\trunk\php\l4-confide\public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:\wamp\www\test\trunk\php\l4-confide\public">
        DirectoryIndex index.php
        Options All Includes Indexes
        Options Indexes FollowSymLinks MultiViews
        Require all granted
    </Directory>
</VirtualHost>

但是我的生产服务器使用的是 Apache 2.2。 "Require all granted" 等价于什么?我不确定这是否会解决问题。我想知道为什么 Apache 只抱怨在 $.ajax 调用中找不到 404 错误,而不是在以 "regular" 方式提交(即保存)表单时(即 POST 通过 HTML).

我猜问题出在 url: '/revocable/users/page/1/update',

假设如果您在页面中 http://localhost/something/ 并使用相关的 url revocable/users/page/1/update 然后 ajax 请求可能是 http://localhost/something/revocable/users/page/1/update and if your in the page http://localhost/another/ 然后请求将是

http://localhost/another/revocable/users/page/1/update

所以你不会在那里找到合适的页面来处理请求。

var csrf = $('meta[name=_token]').attr('content');

        $.ajaxSetup({
            beforeSend: function(request) {
                return request.setRequestHeader('X-CSRF-Token', csrf);
            }
        });

将 var csrf 设置为您的 csrf 变量。我的网站我将其全局存储在元

相当于 Apache 2.2 中的 "Require all granted" 是:

订单允许,拒绝 全部允许

检查 Apache 升级: http://httpd.apache.org/docs/2.4/upgrading.html

此外,.htaccess(2.2 与 2.4)也有一些差异,但我不确定问题是否与此有关。