AJAX 发布到 PHP 为空


AJAX post to PHP empty

我已经检查了其他问题 - 这不是重复的。我已经尝试了我能找到和实施的所有解决方案。

我正在尝试从任务发送数据.php →显示猫.php

任务.php:

<script type="text/javascript">
    $(document).on("click", ".btnCat", function () {
        var filter = $(this).data("id");
        alert(filter);
        $.ajax({
            type: 'POST',
            url: 'showCats.php',
            data: {'filter': filter},
        });
        $('div.container-fluid').load('showCats.php');
    });
</script>

显示猫.php:

$area = $_POST['filter'];
$sql = "select AID,name,surname,street_name,house_number, area, plz,poster,visible from addresses WHERE area LIKE '$area' AND visible LIKE 'show' ORDER BY AID DESC";
$rs = mysqli_query($con,$sql);
$str = '';
while ($res = mysqli_fetch_array($rs)) {
    $str .= '
    <div class="col-md-9">
        <div class="task col-md-12 well" id='.$res['AID'].'>
            <div>
                <button class="btn btn-danger btn-xs btnDelete" id='.$res["poster"].' onclick="refresh()" data-id="'.$res['AID'].'">x</button>
            </div>
            <div>
                <span>'. $res["name"].'</span>
                <span>'. $res["surname"].'</span><br>
                <span>'. $res["street_name"].'</span>
                <span>'. $res["house_number"].'</span><br>
                <span>'. $res["plz"].'</span>
                <span>'. $res["area"].'</span>
            </div>
        </div>
    </div>';
}
echo $str;
?>

var_dump($_POST);返回NULL,即使我可以在Chrome中的开发人员工具下看到帖子值。

我的得到:

Request URL:https://example.com/showCats.php
Request Method:GET
Status Code:200 OK
Remote Address:xxx:443
Response Headers
view source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Fri, 15 Jul 2016 18:43:56 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:nginx/1.6.2
Strict-Transport-Security:max-age=31536000
Transfer-Encoding:chunked
Request Headers
view source
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8,de;q=0.6
Connection:keep-alive
Cookie:PHPSESSID=vudgbb33574tfod2vu48hst830
Host:example.com
Referer:https://example.com/tasks.php
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
X-Requested-With:XMLHttpRequest

我的帖子:

Request URL:https://example.com/showCats.php
Request Method:POST
Status Code:200 OK
Remote Address:xxx:443
Response Headers
view source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Fri, 15 Jul 2016 18:43:56 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:nginx/1.6.2
Strict-Transport-Security:max-age=31536000
Transfer-Encoding:chunked
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,de;q=0.6
Connection:keep-alive
Content-Length:12
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=vudgbb33574tfod2vu48hst830
Host:example.com
Origin:https://example.com
Referer:https://example.com/tasks.php
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
filter:Turgi

您正在尝试从 task.php -> showCats.php 发送数据! 您的代码通过使用以下内容可以很好地做到这一点:

$.ajax({
    type: 'POST',
    url: 'showCats.php',
    data: {'filter': filter},
});

问题是当你这样做时:$('div.container-fluid').load('showCats.php');一个GET请求将被发送到服务器! 所以找到var_dump($_POST)返回 NULL 是正常的。

如果要显示/获取响应,可以像这样使用 success 事件:

$.ajax({
    type: 'POST',
    url: 'showCats.php',
    data: {'filter': filter},
    //A function to be called if the request succeeds.
    success: function(data) {
       $('div.container-fluid').html(data)
   },
   //A function to be called if the request fails
   error: function(xhr, status, error) {
       alert('An error occurred:'+error);
   }
});

设置数据类型参数会告诉您要发送的数据类型。

type: "POST",
dataType: 'json',

我同意上述评论。 再次加载程序不会报告数据库调用的值。 此外,AJAX 通常不会以 json_encode 的回显方式将值返回给调用程序,而不仅仅是将变量回显到未被查看的 PHP 页面运行中吗?