Ajax没有通过post方法传递给php

时间:2018-04-05 04:02:55

标签: php ajax

我在Windows 10上使用谷歌浏览器65.0.3325.181,xampp正在运行php。标题解释了其余部分。

的HTML / PHP:          

    $(document).ready(function (){
        $('#sel_edificio').load('data.php');

        $( ".form-control" ).change(function() {

            var dato = 50;//document.getElementById("sel_edificio").value;

            $.ajax({
                method: "POST",
                data: {'key': dato},
                url: "uno.php",
                success: function(status){
                    var asd = $('#test').load('uno.php');
                    //document.getElementById("NumEstudiantes").value(key);
                }
            });
        });
    });
</script>

uno.php:

<?php
    echo $_POST['key'];
?>

错误:

Notice: Undefined index: key in C:\xampp\htdocs\jqbd\uno.php on line 2

2 个答案:

答案 0 :(得分:0)

将ajax方法改为键入,试试这个

$.ajax({
                type: "POST",
                data: {'key': dato},
                dataType: "json",
                url: "uno.php",
                success: function(status){
                    //var asd = $('#test').load('uno.php');
                    $('#test').load('uno.php', { key: dato });//document.getElementById("NumEstudiantes").value(key);
                }
            });

答案 1 :(得分:0)

您要发送两次请求,请尝试以下操作:

<强> HTML / PHP的

 $(document).ready(function (){

    $('#sel_edificio').load('data.php');

    $( ".form-control" ).change(function() {

        var dato = 50;//document.getElementById("sel_edificio").value;

        $.ajax({
            method: "POST",
            data: {'key': dato},
            url: "uno.php",
            success: function(data){
                $('#test').html(data);
            }
        });
    });
});