无法在 AJAX 中提取 PHP 信息

索引.php →


    <form>      

    <div class="form-group">

        <!-- <input type="text" id="name" class="form-control" placeholder="Enter Name"> -->

    </div>

    <div class="form-group">

        <button type="button" class="btn btn-success" id="btn">Click me I am Neo Anderson</button>

    </div>

</form>

<div id="message"></div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

    $(document).ready(function(){   

        $("#btn").click(function(){

            $.post("ajax.php",function(response){

                // alert(response);

                var message = $("#message").val();

                message.html(response);         

            })

            .fail(function(error){

                alert(error.StatusText);

            })

        })      

    })      

</script>

和 ajax.php →


    <?php 

echo "The ajax post is working";

问题:我无法在我的 ajax 请求中从 ajax.php 文件中获取打印材料。我在哪里步履蹒跚?


慕妹3242003
浏览 104回答 3
3回答

catspeake

你这里有问题:var&nbsp;message&nbsp;=&nbsp;$("#message").val();将那行代码替换为:var&nbsp;message&nbsp;=&nbsp;$("#message");您不想获取 #message div 的值,所以不要使用val()方法。

温温酱

$("#message").html(响应);&nbsp;&nbsp;&nbsp;&nbsp;//orvar message = $(“#message”);message.html(响应);

慕勒3428872

&nbsp;$(document).ready(function(){&nbsp; &nbsp;&nbsp; &nbsp; $("#btn").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; let message = $("#message").val();&nbsp; &nbsp; &nbsp; &nbsp; $.post("ajax.php",{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;msg: message&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // alert(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.html(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; },"json")&nbsp; &nbsp; })&nbsp; &nbsp; &nbsp;&nbsp;})为 JavaScript 试试这个。对于 ajax.php 文件试试这个:<?php&nbsp;echo json_encode("The ajax post is working");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript