How to receive XMLHttpRequest with PHP?

10,749

Solution 1

You will read it exactly the sme way you read normal request vars.

$_GET['varname'] and $_POST['varname']

Solution 2

php://input allows you to read raw POST data like

Welcome <?php print(file_get_contents('php://input')); ?>!<br />

Note: php://input is not available with enctype="multipart/form-data".

Solution 3

You could use fopen() with the input:// wrapper or $HTTP_RAW_POST_DATA.

Solution 4

When you use "method: post" and you want to send a post body, you need the parameter postBody. So something like this might work:

var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{
    contentType: 'text/xml',
    postBody: xml,
    method: 'post',
    onSuccess: function(transport){
        alert(transport.status); alert(transport.responseText);
    }
});

But why did you build a XML doc around your content? You can simply sent the message "heya there" with the postBody without the XML arround.

Edit: Here you'll find all the Ajax.Request options: http://www.prototypejs.org/api/ajax/options

Share:
10,749
screenshot345
Author by

screenshot345

Updated on June 26, 2022

Comments

  • screenshot345
    screenshot345 almost 2 years

    I would like to be able to read XMLHttpRequest that is sent to a PHP page. I am using prototype's Ajax.Request function, and I am sending a simple XML structure.

    When trying to print the POST array on the PHP page, I don't get any output.

    Any help appreciated.

    EDIT: Below is my code

    <html>
    <head>
    
    <SCRIPT type="text/javascript" src="prototype.js"></script>
    
    </head>
    <body>
    
    <script type="text/javascript">
    
    var xml='<?xml version=1.0 encoding=UTF-8?>';
    xml=xml+'<notification>';
    xml=xml+'heya there';
    xml=xml+'</notification>';
    xml=xml+'</xml>';
    
    
    var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{
        contentType: 'text/xml',
        parameters: xml,
        method: 'post',
        onSuccess: function(transport){ alert(transport.status); alert(transport.responseText); }
    });
    
    </script>
    
    </body>
    </html>
    

    post2.php

    Welcome <?php print_r($_POST); ?>!<br />
    
    • screenshot345
      screenshot345 almost 14 years
      Thanks for your reply, please see the edit above for the code.
    • Joseph
      Joseph almost 14 years
      So you are getting Welcome Array()! as your result?
    • screenshot345
      screenshot345 almost 14 years
      Yes Joseph, thats exactly what I'm getting. I was hoping to get the xml back. This needs to be an text/xml content type due to the server that I am forced to use.
  • screenshot345
    screenshot345 almost 14 years
    Unless I'm doing something wrong, I get nothing but an empty array when I print_r $_POST. In my ajax call, my parameter should only be the xml string in the format '<xml>...</xml>, right? Thank you for your input.
  • chris12892
    chris12892 almost 14 years
    You might try to print_r($_GET), just to see if somehow it's getting send via GET.
  • Joseph
    Joseph almost 14 years
    What the Col is referring to is here: us2.php.net/manual/en/…
  • tony gil
    tony gil almost 9 years
    $rawData=file_get_contents('php://input'); // pERFECT upvoted :)
  • tony gil
    tony gil almost 9 years
    like @Adrian i got an empty string.
  • Mark Carpenter Jr
    Mark Carpenter Jr over 6 years
    Straight from the documentation, "Warning This feature was DEPRECATED in PHP 5.6.0, and REMOVED as of PHP 7.0.0."