簡體   English   中英

無法讀取PHP文件返回的Ajax響應

[英]Cannot read the Ajax response returned by PHP file

我是 Ajax 的新手。

我無法從 PHP 文件中得到正確的響應。 控制台中的答案是“未定義”。 當我只是簡單地指向Response變量時,控制台給了我對象; 似乎我無法獲取 PHP 文件結果的值。

這是 HTML 代碼:

<form id="form1" method="post" onsubmit="return send_form(this)">
        <p>Number 1= <input type="text" name="number1"></p>
        <p>Number 2= <input type="text" name="number2"></p>
        <input type="submit" value="calculate">

    </form>
        <script>
           function send_form(form1){
                    let response=$.ajax("1_1.php",{
                    dataType:'json',
                    data:{n1:form1.number1.value, n2:form1.number2.value},
                    }
                    );
                console.log(response.c);

                return false;
            }

        </script>

這是我的 PHP 文件

<?php

$a=$_GET['n1'];
$b=$_GET['n2'];
$c=$a+$b;

echo json_encode(['c'=>$c]);

?>

似乎我不應該使用ajax對變量的賦值。 我試過這種方式,它奏效了:

function send_form(form1){
                    $.ajax("1_1.php",{
                    dataType:'json',
                    data:{n1:form1.number1.value, n2:form1.number2.value},
                    }
                    ).then(process);

                return false;
            }
            function process(result){
                console.log(result.c);
                return false;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM