Javascript AJAX到PHP的post为空
我已经检查了其他问题-这不是重复的。 我已经尝试了我能找到并实施的所有解决方案 我正在尝试从task.php发送数据→ showCats.php task.php:Javascript AJAX到PHP的post为空,javascript,php,ajax,Javascript,Php,Ajax,我已经检查了其他问题-这不是重复的。 我已经尝试了我能找到并实施的所有解决方案 我正在尝试从task.php发送数据→ showCats.php task.php: <script type="text/javascript"> $(document).on("click", ".btnCat", function () { var filter = $(this).data("id"); alert(filter); $.
<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>
我的帖子:
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\u dump($\u 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页面的运行中?将始终返回null,但回显变量。您确定var_dump($\u POST)
的null输出源自您的POST请求吗(ajax
)而不是后续的GET请求(load
)?@showdev我已经试着解释过了,我得到了-2!!!!@ismailbouh我知道。我认为你的回答很好地描述了可能的问题。我只是想澄清一下,你所描述的是OP实际问题的根源。值得一提的是,我建议取消删除你的答案。你已经向showCats.php
发出了两个请求。我不知道为什么要这样做!!请显示两个请求的两个响应(在devtools Chrome下)(一个用于GET
[由.load制作
]和一个用于POST
[由$.ajax制作
])。更多详细信息将告诉您的问题。Ops询问服务器上为什么为空。这不会试图回答这个问题。因为他试图通过使用load
而不是使用success
方法来显示返回的数据!!!我想OP问的不是他的意思。他想知道为什么他的代码不能工作超过w为什么post数组为空。他正在尝试调试。这是他的问题的根源。您必须删除$('div.container-fluid').load('showCats.php')
添加成功后
!请添加错误
函数作为更新的答案,并告诉我结果是什么!@ou_snaaksieQuestion up投票通过。并非所有问题都必须是突破性的。故障排除很重要。不要吹毛求疵,但数据类型
定义了“您期望从服务器返回的数据类型”看。好的,你是对的。我提到它是因为不久前我有一个关于调用的必需和可选部分的问题向我提出。我认为数据类型是必需的。只是想得到一个关于堆栈溢出的答案。
$.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',