简体   繁体   中英

Echo posted Ajax data

I am using AJAX to post some array data to the server. I get the following expected results in the Firebug network console from the Ajax request.

            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)

       $.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. 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?

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 ? .
  2. Maybe serializing will help: make data: JSON.stringify(data) on client and json_decode on server.
  3. Check configurations of your server - are requests you're seeing in your firebug actually reaching the server?

Finally cracked it. Turns out that there was a server side issue with Ajax calls that has now been resolved by the service provider. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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