1

In Safari only, why is the $_POST array empty when posting using a jQuery AJAX call?

IE, Firefox and Chrome all output correctly.

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(
            function()
            {
                $.ajax(
                {
                    type: "POST",
                    url: "target.php",
                    dataType: "xml",
                    data: ({
                        'param1': 'param1'
                    }),
                    success: function()
                    {
                    }
                });
            });
    </script>
    <title></title>
</head>
<body>

</body>
</html>

The file target.php contains the following code:

<?php print_r($_POST); ?>

and outputs the following:

Array
(
)
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
ne0ge0
  • 11
  • 4

4 Answers4

0

Try removing the ( and ) in data.

vher2
  • 980
  • 6
  • 9
0

The problem could be related to iOS 6 caching certain post requests. Have a look at this post: ios caching post request responses.

You could test this by checking if you're using no Cache-Control headers or "Cache-Control: max-age=0" and removing them.

Community
  • 1
  • 1
Mark Roper
  • 1,389
  • 1
  • 16
  • 27
0

First of all you are setting the datatype to xml I guess this should be html since you're not getting xml as responce. see: http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

quote : "Why is data an object in brackets?"

also use async : true in your request if Mark roper is correct.

 $(document).ready(
        function()
        {
            $.ajax(
            {
                type: "POST",
                url: "target.php",
                async :true,
                dataType: "html",
                data: {
                    'param1': 'param1'
                },
                success: function(data) {
            alert(String(data));
            }
            });
        });

Just changed the success to : success: function(data) {alert(String(data));} EDIT: on http://peervantienen.com/Stackoverflow/in-safari-why-is-the-post-array-empty-after-jquery-ajax-call/ it gave me the same results in all browsers.

Peer
  • 1
  • 1
  • I updated the code to the above, and the post array is still empty :( – ne0ge0 Apr 23 '13 at 12:48
  • I'll try to recreate it in a few hours from now. – Peer Apr 23 '13 at 16:02
  • Hi, the solution in this link seems to work in all browsers. can you take a look? http://www.peervantienen.com/Stackoverflow/in-safari-why-is-the-post-array-empty-after-jquery-ajax-call/ . (I'm a bit surprised how php returns the array btw) – Peer Apr 24 '13 at 07:05
0

One of my colleagues found the answer:

http://forums.iis.net/post/1999417.aspx

Hope this helps other people...

ne0ge0
  • 11
  • 4