简体   繁体   English

jQuery的AJAX未发布数据

[英]jQuery's AJAX is not posting data

I want to send the key and value pairs to a PHP file using jQuery's AJAX function, however the function is not sending the data. 我想使用jQuery的AJAX函数将键和值对发送到PHP文件,但是该函数未发送数据。

The PHP code is in the same "Tester.php" file together with the HTML code as shown below: PHP代码与HTML代码位于同一“ Tester.php”文件中,如下所示:

<?php
if (array_key_exists("REQUEST_METHOD", $_SERVER) && $_SERVER["REQUEST_METHOD"] == "POST") {
    echo "<pre>";
    print_r($_POST); // always empty, no clue why!
    echo "</pre>";
    exit();
}
?>

<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "Tester.php", // the same file/page
                data: {
                    requestData: true,
                    message: "please print me!"
                },
                success: function(data) {
                    document.write("success!");
                    document.write(data);
                },
                error: function(xmlHttp) {
                    document.write("error!");
                    document.write(xmlHttp.responseText);
                }
            });
        });
    </script>
</head>
<body>
    <p>Testing...</p>
</body>
</html>

This prints: 打印:

success!
Array
(
)

But the array printed should contain the "requestData: true" from the data passed to the $_POST array, but instead this array is empty. 但是打印的数组应包含传递给$ _POST数组的数据中的“ requestData:true”,但该数组为空。 What have I done wrong? 我做错了什么? Thank you! 谢谢!

Html File (36516400.html) HTML文件(36516400.html)

<html>
    <head>
        <title>36516400</title>
        <script type="text/javascript" src="../../../assets/js/script.js"></script>
    </head>

    <body>
        <h1>Welcome</h1>
        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    type:'POST',
                    data:{
                        'requestData':true,
                        'message':"please print me!"
                    },
                    url:'responce.php',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    success:function(data){
                        alert(data);
                    }
                });
            })
        </script>
    </body>
</html>

PHP file (responce.php) PHP文件(responce.php)

<?php
    echo "<pre>";
    print_r($_REQUEST);
    echo "<pre>";
?>

Request in chrome console 在Chrome控制台中请求

在此处输入图片说明

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

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