简体   繁体   English

无法接收通过AJAX POST发送到PHP的值

[英]Not recieving values sent via AJAX POST to PHP

I'm trying I'm trying to send a specific section of my URL string, to my php file, to then populate my page accordingly. 我正在尝试将URL字符串的特定部分发送到php文件,然后相应地填充我的页面。 Everything is working fine, except for the AJAX POST method. 除AJAX POST方法外,其他一切都正常。 I've tried doing var_dump of the POST variable in my PHP, and my array is empty (so I know nothing is getting through). 我已经尝试在PHP中执行POST变量的var_dump,并且我的数组为空(因此我知道没有任何结果)。

The success DOES return as being passed, so I don't know where the data is going. 成功并不会随着传递而返回,所以我不知道数据在哪里。 I'm testing locally on XAMPP, and I've combed through SoF and no luck on any of the fixes. 我正在XAMPP上进行本地测试,并且已经通过SoF进行了梳理,但在任何修复程序上都没有运气。 My code is below. 我的代码如下。

Screen shot of page: 页面的屏幕截图:

在此处输入图片说明

jQuery AJAX Request: jQuery AJAX请求:

$(document).ready(function() {
    str = window.location.href;
    pos = str.search("pages/"); //42
    send = str.slice(42, -5);
    console.log(send);
    console.log(pos);
    $.ajax({
        type: "POST",
        url: "retrieve.php",
        data: {
            tom: send
        },
        success: function() {
            $.get("retrieve.php", function(data, status) {
                    $("#main").html(data);
                }) //ends GET function   
        },
        error: function() {
            console.log(arguments)
        }
    }); //ends POST request               
}); //ends DOC-READY function

PHP: PHP:

echo "<i>hello</i>";
echo var_dump($_POST);
$url = $_POST['tom'];
json_decode($url);
echo $url;

Try the below, 试试下面的,

Also you don't need to json_decode unless you send a json request,And please make sure all the post values are passing. 另外,除非您发送json请求,否则不需要json_decode ,并且请确保所有post值都已传递。

Ajax: 阿贾克斯:

$(document).ready(function() {
    var str = window.location.href;
    var pos = str.search("pages/"); //42
    var send = str.slice(42, -5);
    console.log(send);
    console.log(pos);
    $.ajax({
        type: "POST",
        url: "retrieve.php",
        data: {
            'tom': send//make sure this is not empty
        },
        success: function(data) {
            console.log(data);
        },
        error: function(arguments) {
            console.log(arguments)
        }
    }); //ends POST request               
}); //ends DOC-READY function

PHP: PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //echo "<i>hello</i>";
    //echo var_dump($_POST);
    $url = $_POST['tom'];
    json_encode($url);
    echo $url;
}

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

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