简体   繁体   English

Echo发布了Ajax数据

[英]Echo posted Ajax data

I am using AJAX to post some array data to the server. 我正在使用AJAX将一些数组数据发布到服务器。 I get the following expected results in the Firebug network console from the Ajax request. 我从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"

for infor this is the ajax call which give the expected success message (same as the firebug param output) 因为这是ajax调用,它给出了预期的成功消息(与firebug param输出相同)

       $.post({

        data: data,

         type: 'POST',

        url: 'drag_data.php?',

        success:function(result){
        $(".result").html(data);},

        error: function(){
        console.log(arguments);
        }
    });

I just want to echo the posted data in the drag_data.php script. 我只想在drag_data.php脚本中回显发布的数据。 I have tried the following test code (as well as (print_r and var_dump) but cannot see any posted data which has baffled me. Can anyone tell me what I am doing wrong please? 我已经尝试了以下测试代码(以及(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' , with preceding slash and without ? make url: '/drag_data.php' ,前面有斜线而没有? .
  2. Maybe serializing will help: make data: JSON.stringify(data) on client and json_decode on server. 也许序列化将有助于:在客户端上生成data: JSON.stringify(data) ,在服务器上json_decode
  3. Check configurations of your server - are requests you're seeing in your firebug actually reaching the server? 检查服务器的配置 - 您在firebug中看到的请求是否实际到达服务器?

Finally cracked it. 最后破解了它。 Turns out that there was a server side issue with Ajax calls that has now been resolved by the service provider. 事实证明,服务提供商现在已经解决了Ajax调用的服务器端问题。 So in actual fact my original code works as it should. 所以实际上我的原始代码应该正常工作。 Maybe this thread or the code will be useful for someone else in the future. 也许这个帖子或代码将来对其他人有用。

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

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