简体   繁体   English

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

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

The AJAX request goes through correctly, I checked with chrome's developer tools, there is a request on quiz.php page, but when I check for $_POST['risultato'] it looks doesn't exist. AJAX 请求正确通过,我检查了 chrome 的开发人员工具,在 quiz.php 页面上有一个请求,但是当我检查 $_POST['risultato'] 时,它看起来不存在。 I noticed though that in Chrome's dev tools there's 2 quiz.php elements (one xhr the other document)我注意到虽然在 Chrome 的开发工具中有 2 quiz.php 元素(一个 xhr 另一个文档)

I tried changing the code in several ways, but it seems like it doesn't work我尝试以多种方式更改代码,但似乎不起作用

<?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");
                }
            })
    }

The program is expected to return the result on quiz.php page (the same page where ajax request is fired), and it's supposed to print it somewhere该程序应在 quiz.php 页面(触发 ajax 请求的同一页面)上返回结果,并且应该将其打印在某处

EDIT: I fixed it编辑:我修好了

<?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);
});

In Ajax, the data attribute is in JSON format.在 Ajax 中,数据属性采用 JSON 格式。 your data attribute will be like this您的数据属性将是这样的

data: {risultato: ris}

Hi you can do it this way:嗨,你可以这样做:

your php script:你的 php 脚本:

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

Where action is a command you want to do SLC, UPD, DEL etc and risultato is a parameter其中 action 是您想要执行 SLC、UPD、DEL 等的命令,而 risultato 是一个参数

then in your ajax:然后在你的 ajax 中:

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

}

call it like this:像这样称呼它:

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

Hope it helps希望能帮助到你

HTML CODE代码

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

                }
            })
        });
    </script>

PHP CODE PHP代码

<?php

file_get_contents('php://input');

print($_POST['risultato']);

Console Output控制台输出

在此处输入图片说明

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

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