我正在使用 AJAX 将一些数组数据发送到服务器。我从 Ajax 请求在 Firebug 网络控制台中得到以下预期结果。
POST -----> http://example.com/drag_data.php
//request header
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://example.com/drag.php
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 90
Cookie: PHPSESSID=b1lr9he4l2hbcnlkcsebfq2134
Connection: keep-alive
//data in the request body
item[]=1&item[]=3&item[]=2&item[]=4&item[]=5
//firebug params
item[]:"1"
item[]:"3"
item[]:"2"
item[]:"4"
item[]:"5"
对于infor来说,这是一个ajax调用,它给出了预期的成功消息(与firebug参数输出相同)
$.post({
data: data,
type: 'POST',
url: 'drag_data.php?',
success:function(result){
$(".result").html(data);},
error: function(){
console.log(arguments);
}
});
我只想回显drag_data.php 脚本中发布的数据。我已经尝试了以下测试代码(以及(print_r 和 var_dump)),但看不到任何已发布的数据,这让我感到困惑。任何人都可以告诉我我做错了什么吗?
drag_data.php test file
$i = 0;
//this loop is failing to echo the posted array data from the Ajax request
foreach ($_POST['item'] as $value) {
echo "each".$value;
$i++;
}
?>
最佳答案
- 制作
url: '/drag_data.php'
,前面带斜杠,不带?
。 - 也许序列化会有所帮助:在客户端上制作
data: JSON.stringify(data)
并在服务器上制作json_decode
。 - 检查服务器的配置 - 您在 Firebug 中看到的请求是否确实到达了服务器?
关于javascript - Echo 发布 Ajax 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40984908/