Php 如何修复服务器can';t访问jquery发送的$\u POST数据
我使用Jquery AJAX将数据发送到服务器,但无法作为Php 如何修复服务器can';t访问jquery发送的$\u POST数据,php,jquery,Php,Jquery,我使用Jquery AJAX将数据发送到服务器,但无法作为$\u POST变量访问它们。我做错了什么? PHP返回的错误是,$\u POST['name']未定义 JS代码 $(document).ready(function() { function load_friend_request() { $.ajax({ url: "/friend_requests_main_loader.php", type: 'POST',
$\u POST
变量访问它们。我做错了什么?
PHP返回的错误是,$\u POST['name']未定义
JS代码
$(document).ready(function() {
function load_friend_request() {
$.ajax({
url: "/friend_requests_main_loader.php",
type: 'POST',
data: { name: 'Kay'},
success: function(html) {
//$("#friend_request:last-child").empty();
$("#friend_request").append(html);
}
})
}
setInterval(function() {
load_friend_request();
}, 2000);
});
</script>```
PHP CODE
`
<?php
echo $_POST['name'];
?>`
$(文档).ready(函数(){
函数加载\朋友\请求(){
$.ajax({
url:“/friend\u requests\u main\u loader.php”,
键入:“POST”,
数据:{name:'Kay'},
成功:函数(html){
//$(“#朋友请求:最后一个孩子”).empty();
$(“#好友请求”).append(html);
}
})
}
setInterval(函数(){
加载朋友请求();
}, 2000);
});
```
PHP代码
`
`
$(文档).ready(函数(){
函数加载\朋友\请求(){
$.ajax({
键入:“POST”,
url:“/query\u loader.php”,
数据:{friend_count:'count'},
成功:函数(html){
$(“.friend count”).remove();
$(html).insertAfter(“#朋友请求#移动”);
$(html).insertAfter(“#友人请求”);
}
})
}
setInterval(函数(){
加载朋友请求();
}, 2000);
});
类型、post和url对象的顺序非常重要。我不知道我是否正确。也显示php代码。显示的是什么
print\r($\u POST)
?我找到了一个解决方案,我认为Jquery对象的顺序很重要…检查它发送的标题。。。如果是普通的帖子,或者可能带有JSON标题。您可以使用选项:dataType来设置它。如果是JSON wo,则需要从中获取数据php://input t类似于json解码(文件获取内容)php://input"))
<script>
$(document).ready(function() {
function load_friend_request() {
$.ajax({
type: 'POST',
url: "/query_loader.php",
data: { friend_count: 'count'},
success: function(html) {
$(".friend-count").remove();
$(html).insertAfter("#friend_request_mobile");
$(html).insertAfter("#friend_request");
}
})
}
setInterval(function() {
load_friend_request();
}, 2000);
});
</script>