Javascript 无法在AJAX中提取PHP信息

Javascript 无法在AJAX中提取PHP信息,javascript,php,ajax,Javascript,Php,Ajax,Index.php→ <form> <div class="form-group"> <!-- <input type="text" id="name" class="form-control" placeholder="Enter Name"> --> </div> <d

Index.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>

点击我,我是尼欧·安德森
$(文档).ready(函数(){
$(“#btn”)。单击(函数(){
$.post(“ajax.php”,函数(响应){
//警报(响应);
var message=$(“#message”).val();
html(响应);
})
.失败(功能(错误){
警报(错误。状态文本);
})
})      
})      
和ajax.php→

    <?php 
echo "The ajax post is working";

您在这里遇到了一个问题:

var message = $("#message").val();
将该行代码替换为以下内容:

var message = $("#message");

您不想获取#message div的值,因此不要使用
val()
方法。

您遇到了以下问题:

var message = $("#message").val();
 $(document).ready(function(){   
    $("#btn").click(function(){
        let message = $("#message").val();
        $.post("ajax.php",{
         msg: message
        },
        function(response){
            // alert(response);
            message.html(response);         
        },"json")
    })      
})
将该行代码替换为以下内容:

var message = $("#message");
您不想获取#message div的值,因此不要使用
val()
方法

 $(document).ready(function(){   
    $("#btn").click(function(){
        let message = $("#message").val();
        $.post("ajax.php",{
         msg: message
        },
        function(response){
            // alert(response);
            message.html(response);         
        },"json")
    })      
})
Essayer ceci pour le JavaScript。 Et pour le fichier ajax.php essayer ceci:

<?php 
echo json_encode("The ajax post is working");

Essayer ceci pour le JavaScript。
Et pour le fichier ajax.php essayer ceci:

<?php 
echo json_encode("The ajax post is working");
$(“#消息”).html(响应)

var message=$(“#message”)

html(响应)

$(“#消息”).html(响应)

var message=$(“#message”)


html(响应)

我对使用JSON感兴趣。我对使用JSON感兴趣。如果您发出post请求,您应该发出get请求以获取data@RohitAmbre我假设作者缩短了代码示例,并且在ajax.php文件中会有更多操作。表单的结构(已注释掉的名称字段)表明将有某种保存操作。因此,post请求可能没问题。您正在发出post请求,您应该发出get请求以获取data@RohitAmbre我假设作者缩短了代码示例,并且在ajax.php文件中会有更多操作。表单的结构(已注释掉的名称字段)表明将有某种保存操作。所以post请求可能没问题。