jQuery重定向中的POST变量,但未设置 [英] POST variable from jquery redirecting but is not set

查看:96
本文介绍了jQuery重定向中的POST变量,但未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我问了一个问题,关于将jQuery的POST变量发送到另一个php页面,重定向到该页面,对所发送的信息进行一些操作以及重定向到原始页面.

I asked a question recently about sending a POST variable off of jquery to another php page, redirecting to that page, doing some action with the sent information, and redirecting off of the original page.

jQuery post方法和目录混乱

我几乎在完全相同的代码上寻求帮助,因为对此进行评论的人只回答了我一半的问题,并将其标记为重复.虽然确实有帮助,但并不能解决问题.

I am asking for help on almost the exact same code, since the person who commented on it only answered half of my question and marked it as a duplicate. While it did help, it did not solve the problem.

我已根据先前尝试解决此问题的建议进行了编辑.如您所见,在本示例中,单击保存按钮将带您进入changePage.inc.php,这是第一个重定向,但不会发生第二个重定向,应该将您带到secondPage.php.

I have made edits according what was suggested in the previous attempt to get this problem solved. As you will see, hitting the save button in this example will bring you to changePage.inc.php, the first redirect, but the second redirect does not happen, which should bring you to 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/saveEssay.inc.php" method="POST">
  <button type="submit" id="saveButton">Save!</button>    
</form>
</body>
</html>

名为changePage.inc.php的文件(在名为include的文件夹中)

<?php

session_start();

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

exit();

名为secondPage.php的文件

<?php
    echo 'Hello World';
?>

推荐答案

jQuery $.post()方法就是所谓的asynchronous调用.这意味着$.post()试图执行时,它后面的任何代码行都将尝试执行.

The jQuery $.post() method is what is referred to as an asynchronous call. What this means is that while $.post() is trying to execute, any line of code after it will try to execute.

请注意,您在$.post()之后立即进行重定向.这意味着在尝试运行$.post()时,您将重定向到includes/changePage.inc.php,这可能会阻止PHP重定向到../secondPage.php的运行.这就是我们所说的race condition,其中两个事件同时运行,并且其中一个事件可能在另一个事件之前完成,而无法保持事件的同步.

Note that you're redirecting immediately after your $.post(). This means that while $.post() is trying to run, you're redirecting to includes/changePage.inc.php, which is probably preventing the PHP redirect to ../secondPage.php from running. This is what we call a race condition, where two events are running simultaneously and either one could finish before the other with no way to keep the events synchronized.

据我所知,您真正想做的是将数据处理交给PHP,然后在完成后,您想要重定向到给定的页面.

What you're really trying to do, from what I can tell, is hand off processing of your data to PHP, then upon finishing, you want to redirect to a given page.

如果我对此是正确的,请删除window.location.href = "includes/changePage.inc.php";行,因为否则您总是会遇到这种竞争状况的问题.处理PHP中的重定向-它应独立工作.如果您无法直接在PHP中进行重定向,则可以通过jQuery进行重定向:

If I'm correct about this, then remove the window.location.href = "includes/changePage.inc.php"; line because otherwise you're always going to run into issues with this race condition. Handle the redirect in PHP--it should work on its own. If you're having trouble with redirecting directly in PHP, then you can redirect through jQuery:

您的jQuery POST

$.post("includes/changePage.inc.php", {message: message}, function(data) {
    var obj = $.parseJSON(data);
    window.location.href = obj.redirect_url;
});

通过PHP返回的内容

echo json_encode(array('redirect_url'=>'includes/secondPage.php'));

如果这不能满足您的需求,那么您将需要坐下来,并更好地解释您的用例和意图.如果您不能很好地解释它来准确传达您想要的内容,那么您可能对要解决的问题没有完全的了解.

If this doesn't meet your needs, then you'll need to really sit down and better explain your use case and your intent. If you can't explain it well enough to communicate exactly what you want, then you probably don't have a complete grasp of the problem you need to solve.

这篇关于jQuery重定向中的POST变量,但未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆