簡體   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