繁体   English   中英

AJAX 请求已发送,但 $_POST 未更新

[英]AJAX request is sent, but $_POST doesn't update

AJAX 请求正确通过,我检查了 chrome 的开发人员工具,在 quiz.php 页面上有一个请求,但是当我检查 $_POST['risultato'] 时,它看起来不存在。 我注意到虽然在 Chrome 的开发工具中有 2 quiz.php 元素(一个 xhr 另一个文档)

我尝试以多种方式更改代码,但似乎不起作用

<?php
    if(isset($_POST['risultato'])){
        print($_POST['risultato']);
    }
?>

<script>    
    function inviaRisultati(ris){
            $.ajax({
                url: "quiz.php",
                type: "POST",
                cache: false,
                data: {risultato: ris},
                success: function(){
                    alert("INVIATI");
                }
            })
    }

该程序应在 quiz.php 页面(触发 ajax 请求的同一页面)上返回结果,并且应该将其打印在某处

编辑:我修好了

<?php
    file_get_contents('php://input');
    if(isset($_POST['risultato'])){
        print($_POST['risultato']);
    }
?>

function inviaRisultati(param) {
   return $.ajax({
         url:"quiz.php",
         method:"POST",
         data:{action: "SLC", risultato :param},
         dataType:"text"
    });

}

inviaRisultati(1).done(function(response){``
    document.open(); 
    document.write(response);
});

在 Ajax 中,数据属性采用 JSON 格式。 您的数据属性将是这样的

data: {risultato: ris}

嗨,你可以这样做:

你的 php 脚本:

     if (isset($_POST["action"])) {
    $action = $_POST["action"];
    switch ($action) {
        case 'SLC':
        if (isset($_POST["risultato"])) {
            $response = $_POST["risultato"];
            echo $response;
        }
    }
    break;
}

其中 action 是您想要执行 SLC、UPD、DEL 等的命令,而 risultato 是一个参数

然后在你的 ajax 中:

 var param = $('#yourinputid').val();
function getInfo(param) {
   return $.ajax({
         url:"quiz.php",
         method:"POST",
         data:{action: "SLC", risultato :param},
         dataType:"text"
});

}

像这样称呼它:

getInfo(param).done(function(response){
alert(response);
//do something with your response here
})

希望能帮助到你

代码

<script>
        jQuery(document).ready(function(){
            ris = 'abcd';
            jQuery.ajax({
                url: "quiz.php",
                type: "POST",
                cache: false,
                data: {risultato: ris},
                success: function(){

                }
            })
        });
    </script>

PHP代码

<?php

file_get_contents('php://input');

print($_POST['risultato']);

控制台输出

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM