【问题标题】:Post variable before redirect?在重定向之前发布变量?
【发布时间】:2015-10-15 13:16:15
【问题描述】:

我目前正在构建一个 CMS。我想在重定向时将 page-id 或 site-id 发送到下一页。我尝试使用 jQuery POST 函数:

    $(document).ready(function(){
    $('.sender').on('click','a',function(){
                    var url = $(this).attr('href');
                    var n = url.indexOf('#')+1;
                    var siteid = url.substr(n,url.length);

                    $.ajax({
                        url: 'pages.php',
                        type: 'POST',
                        data: { siteid:siteid },
                        success: function(response){
                                console.log('check'); 
                        },
                        error: function(){
                                console.log('error');
                        }
                    });
                });
});

但是因为请求是和重定向同时发送的,所以好像不起作用。

因为我使用 apache rewrite_engine 来重定向东西,所以我不能使用 GET。

除了 session_variables,我还有哪些选择? 我想保证它的安全,所以我不希望太多信息可见/可用!

【问题讨论】:

    标签: php jquery redirect


    【解决方案1】:

    要实现这一点,您需要等待 AJAX 请求完成页面被重定向。试试这个:

    $(document).ready(function(){
        $('.sender').on('click', 'a', function(e){
            e.preventDefault(); // stop the default redirect
            var url = $(this).attr('href');
            var n = url.indexOf('#') + 1;
            var siteid = url.substr(n, url.length);
    
            $.ajax({
                url: 'pages.php',
                type: 'POST',
                data: { 
                    siteid: siteid 
                },
                success: function(response){
                    console.log('check'); 
                    window.location.assign(url); // redirect once the AJAX has successfully completed
                },
                error: function(){
                    console.log('error');
                }
            });
        });
    });
    

    【讨论】:

      【解决方案2】:

      我不确定你为什么需要,但我认为这可以解决你的问题。

      如果您需要使用真正的 POST 方法传输信息,只需使用 method="POST" 创建一个隐藏表单并在点击事件中填写它。

      <script type="text/javascript">
      $(document).ready(function(){
          $('.sender').on('click','a',function(event){
              event.preventDefault();
      
              var url = $(this).attr('href');
              var n = url.indexOf('#')+1;
              var siteid = url.substr(n,url.length);
      
              $('input[name="siteid"]').val(siteid);
              $('#redirectForm').submit();
          });
      });
      </script>
      
      <form id="redirectForm" action="pages.php" method="POST">
          <input type="hidden" name="siteid" value=""/>
      </form>
      

      【讨论】:

        猜你喜欢
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 2014-05-22
        • 2020-08-17
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多