代码之家  ›  专栏  ›  技术社区  ›  je123

$.post()可能的竞争条件;重定向问题

  •  0
  • je123  · 技术社区  · 8 年前

    我有一个索引。php页面,我想从textArea中的用户那里接收信息,点击save后,变量应该被发布到另一个名为changePage的页面。inc.php。在另一个页面上设置post变量后,将显示changePage。股份有限公司.php应该从原始索引重定向。php到一个名为secondPage的页面。php。

    名为index的文件。php

    <?php
        session_start();
    ?>
    
    <!DOCTYPE html>
    <html>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"
        integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous">
    </script>
    <script>
        $(document).ready(function(){
            $('#saveForm').submit(function(event){
                event.preventDefault();
                var message = "";
    
                var changeText = function(){
                    message = "You wrote: " + $('#text').val();
                };
    
                var send = function(){
                    if(message !== ""){
                        $.post("includes/changePage.inc.php", {message: message});
                        window.location.href = "includes/changePage.inc.php";
                    }
                };
    
                changeText();
                send();
            });
        });
    </script>
    <body>
    <textArea id="text"></textArea>
    <form id="saveForm" action="includes/changePage.inc.php" method="POST">
      <button type="submit" id="saveButton">Save!</button>    
    </form>
    </body>
    </html>
    

    文件名更改页面。inc.php

    <?php
    
    session_start();
    
    if(isset($_POST['message'])){
        header("Location: ../secondPage.php");
    }
    
    exit();
    

    名为secondPage的文件。php

    <?php
        echo 'Hello World';
    ?>
    

    有了现在的代码,它就进入了changePage。股份有限公司.php,但不是第二页。php。我认为post变量要么根本没有设置,要么设置得太晚( 也许这是因为比赛条件

    我以前问过这个问题,我认为答案是在$内使用函数(数据)。post()方法创建具有$的对象。parseJSON(数据)将起作用。但是在运行代码时,产生了错误,所以现在我再次询问。这是之前的尝试: POST variable from jquery redirecting but is not set

    2 回复  |  直到 8 年前
        1
  •  0
  •   Barmar    8 年前

    重定向到 secondPage.php 在Javascript中,而不是在PHP中。并在的回调函数中执行 $.post 因此它等待脚本完成。

            var send = function(){
                if(message !== ""){
                    $.post("includes/changePage.inc.php", {message: message}, function() {
                        window.location.href = "includes/secondPage.php";
                    });
                }
            };
    

    但更简单的方法是添加 message 作为表单中的输入,并让表单正常提交。

    $("#saveForm").submit(function() {
      $("#message").val("You wrote: " + $("#text").val());
    }
    <textArea id="text"></textArea>
    <form id="saveForm" action="includes/changePage.inc.php" method="POST">
      <input type="hidden" name="message" id="message">
      <button type="submit" id="saveButton">Save!</button>    
    </form>
        2
  •  0
  •   clemens321    8 年前

    不要在触发AJAX请求后就离开页面,使用回调等待结果。

    $.post("includes/changePage.inc.php", {message: message}, function (data) {
        // here you see 'Hello World' in your browser console
        console.log(data);
        // here you don't see anything in your browser window because it is no POST
        window.location.href = "includes/changePage.inc.php";
    });
    

    但是 要触发ajax请求并随后重定向到同一页面。。。

    也许这就是我们想要的(第二次尝试):

    <html>
      <head>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"
            integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
            crossorigin="anonymous">
        </script>
      </head>
      <body>
        <script>
    $(document).ready(function(){
        $('#saveForm').submit(function(event){
            // NO: event.preventDefault();
            var message = "";
    
            var changeText = function(){
                message = "You wrote: " + $('#text').val();
            };
    
            changeText();
    
            // Check for text as message can't be empty here
            if (!$('#text').val()){
                // preventDefault() here if text is empty
                event.preventDefault();
            }
        });
    });
        </script>
        <form id="saveForm" action="includes/changePage.inc.php" method="POST">
          <!-- put textarea inside your form, and write tags in lowercase -->
          <textarea id="text"></textarea>
          <button type="submit" id="saveButton">Save!</button>    
        </form>
      </body>
    </html>
    
    推荐文章