Echo发布了Ajax数据

时间:2016-12-05 23:00:38

标签: javascript php jquery ajax request

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

forfor这是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++;
                }
                ?>

2 个答案:

答案 0 :(得分:0)

  1. 使用前面的斜杠和url: '/drag_data.php'制作?
  2. 也许序列化会有所帮助:在客户端上制作data: JSON.stringify(data),在服务器上制作json_decode
  3. 检查服务器的配置 - 您在firebug中实际到达服务器的请求是什么?

答案 1 :(得分:0)

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