繁体   English   中英

Echo发布了Ajax数据

[英]Echo posted Ajax data

我正在使用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"

因为这是ajax调用,它给出了预期的成功消息(与firebug param输出相同)

       $.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++;
                }
                ?>
  1. make url: '/drag_data.php' ,前面有斜线而没有?
  2. 也许序列化将有助于:在客户端上生成data: JSON.stringify(data) ,在服务器上json_decode
  3. 检查服务器的配置 - 您在firebug中看到的请求是否实际到达服务器?

最后破解了它。 事实证明,服务提供商现在已经解决了Ajax调用的服务器端问题。 所以实际上我的原始代码应该正常工作。 也许这个帖子或代码将来对其他人有用。

暂无
暂无

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

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