简体   繁体   English

Ajax不通过post方法传递给php

[英]Ajax not passing by post method to php

Im using google chrome 65.0.3325.181 on windows 10, xampp is on to run the php. 我在Windows 10上使用google chrome 65.0.3325.181,xampp可以运行php。 title explains the rest. 标题说明了其余部分。

html/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: uno.php:

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

error: 错误:

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

change ajax method to type, try this 更改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);
                }
            });

You are sending request two time, try this: 您两次发送请求,请尝试以下操作:

html/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(data){
                $('#test').html(data);
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM