【问题标题】:How to fix 301 Moved permanently for Ajax request如何为 Ajax 请求永久修复 301 Moved
【发布时间】:2012-12-29 14:58:34
【问题描述】:

我的 ajax 请求在我的主机上以完美的方式运行,但每当应用程序安装在服务器上时,所有请求的状态都是“301 永久移动”。

我已经调试了 3 天,但没办法。我不知道为什么我会得到这个。我正在使用 Symfony2。

这是服务器端的请求:

$('#edit').click(function () {
    var $validEmail = $('#mail').validator({
        type: 'error',
        rules: [{
            control: 'email',
            message: 'Format invalide'
        }]
    });
    var $validpost_code = $('#post_code').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Code postal obligatoire'
        }]
    });
    var $validRs = $('#rs').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Raison sociale obligatoire'
        }]
    });
    var $validAddr = $('#addr').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Adresse obligatoire'
        }]
    });
    var $validPhone_number = $('#phone_number').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Tel obligatoire'
        }]
    }) && $('#phone_number').validator({
        type: 'error',
        rules: [{
            control: 'phone',
            message: 'Format requise mask: +33 # ## ## ## ##'
        }]
    });

    var $validCountry = $('#country').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Ville obligatoire'
        }]
    });
    var $validTown = $('#town').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Pays obligatoire'
        }]
    });
    var $valid3897 = $('#3897').validator({
        type: 'error',
        rules: [{
            control: 'required',
            message: 'Forme juridique obligatoire obligatoire'
        }]
    });
    if ($validEmail && $validpost_code && $validRs && $validAddr && $validPhone_number && $validTown && $valid3897 && IsPosInteger($('#post_code').find('input').val())) {
        var data = {
            name: $('#name').find('input').val(),
            id: "{{client.id}}",
            post_code: $('#post_code').find('input').val(),
            rs: $('#rs').find('input').val(),
            address: $('#addr').find('input').val(),
            email: $('#mail').find('input').val(),
            effectif: $('#efectif').find('input').val(),
            turnover: $('#turnover').find('input').val(),
            siteWeb: $('#site').find('input').val(),
            phone_number: $('#phone_number').find('input').val(),
            country: $('#country').find('input').val(),
            town: $('#town').find('input').val(),
            fax: $('#number').find('input').val(),
            kbis: $('#bis').find('input').val(),
            vat: $('#vat').find('input').val(),
            legal_form: $('#3897').find('select').val(),
            active: $('#active').attr('checked'),
            groupe: $('#groupe').find('input').val(),
            business_sector: $('#sa').find('input').val()
        };
        $("#site_content").block();
        $.ajax({
            url: "{{url('Update_client')}}",
            type: "POST",
            dataType: 'json',
            data: {
                data: ODEANCE.toJson(data)
            },
            success: function (updateResponse) {
                if (updateResponse) {
                    $("#site_content").unblock(),
                    jAlert('Modification effectuée  avec sucées ', 'Modification') //,
                    //document.location=document.location
                }
            }
        });
    } else {
        jAlert('Veuillez vérifier les formats de vos champs', 'Modification');
    }
});

这里是控制器中的方法:

public function updateClientAction() {
    try {
        //Getting sended data 
        $data = $this->getRequest()->get('data');
        $data = \json_decode(\stripslashes($data));
        $spi = $this->get('spi');
        $clientManager = $spi->getClientManager();
        $updateResponse =($clientManager->update($data) == true ) ?  1 : 0;
        return new Response($updateResponse);
    } catch (\Exception $ex) {
        $updateResponse = 0;
        return new Response($updateResponse);
    }
}

请求的响应似乎无法返回(请求的)原始位置。

【问题讨论】:

    标签: ajax symfony http-status-code-301


    【解决方案1】:

    从 routing.yml 中的模式中删除尾部斜杠

    【讨论】:

      【解决方案2】:

      如果这对其他人有帮助 - 我刚刚遇到了同样的问题,并通过使用 path() 而不是 url() 解决了它。使用 url() 调用被定向到 http,然后被重定向到 https,这很可能导致 301。使用 path() 调用被直接定向到 https。

      【讨论】:

        【解决方案3】:

        几天后,问题现在解决了。这是 http 和 https 请求之间的冲突,所以我所做的就是用这条线强制我的应用程序到 https 端是 security.yml

        access_control:        
              - { path: ^/,role: ROLE_USER, requires_channel: https }
        

        谢谢大家^^

        【讨论】:

          【解决方案4】:
          1. 尝试直接浏览{{url('Update_client')}}
          2. 如果{{url('Update_client')}} 的输出正确,请检查您的html 源代码

          【讨论】:

          • 不,它不起作用。一直在我的主机上运行的问题。会不会是缓存的问题?请求头显示Cache-Control private
          • 你说所有的请求都返回301,是不是你的每一页都是申请?看起来更像是一个服务器配置。
          • 是的,所有的ajax请求都被移动了:/问题是不是我在服务器上安装了它,但我必须调试它,经理说问题应该在我这边解决,但我有不知道会是什么:/这就是为什么我说这可能是缓存的问题
          • 据我所知,缓存控制指令不是问题,私有意味着它只能为每个用户缓存(浏览器缓存)。并且通常与 301 重定向无关。您仍然可以尝试清除浏览器缓存,看看是否有帮助。这些应用程序 URL 是您正在创建的新 URL,还是它们以前存在?
          • 感谢您的回复 :) 是的,我清除了浏览器缓存,但问题始终存在。对于它们已经存在的 URL,但我从 google:p 了解到的可能是 new Response() 的返回不知道去哪里,这意味着请求不会保留引用页面,但我不知道为什么
          猜你喜欢
          • 2015-08-11
          • 2020-06-04
          • 1970-01-01
          • 1970-01-01
          • 2014-12-30
          • 1970-01-01
          • 1970-01-01
          • 2017-02-08
          • 1970-01-01
          相关资源
          最近更新 更多