Ajax post- Cross domain request XDR

This is my JavaScript code to make XDR post request:

if(isIE()) {
    xdr = new XDomainRequest();
    if (xdr) {
        xdr.onerror = err;
        xdr.ontimeout = timeo;
        xdr.onload = loadd;
        xdr.timeout = 10000;
        xdr.open('POST', url);

Open in new window

And when I try to access the variable myval in PHP:

if(isset($HTTP_RAW_POST_DATA)) {
  parse_str($HTTP_RAW_POST_DATA); // here you will get variable $myval
  if($myval== 'abc') {
  echo "TRUE !";
else {
 echo 'NO POST DATA';

Open in new window

I get the output as 'NO POST DATA'. Could anyone help me in finding what is wrong here? Many thanks.
Who is Participating?
gr8gonzoConnect With a Mentor ConsultantCommented:
1. Make sure you're following all the rules here:

2. $HTTP_RAW_POST_DATA is not enabled / available by default. You need to have always_populate_raw_post_data enabled in your php.ini file:
(and most setups need to restart PHP/Apache for php.ini changes to take effect)

Alternatively, you can use the php://input stream, which will give you the same data without incurring the performance cost of forcing PHP to always make that raw data available, even if you don't need to use it on other pages.

Courtesy of Ray Paseur's comment on php.net:
<?php $postdata = file_get_contents("php://input"); ?>

Open in new window

That said, the $_POST array is probably what you're looking for, not the raw POST data. For example:

  echo $_POST["myval"]; // Should display abc

Open in new window

Now, if the intention is to post with the multipart/form-data encoding (usually when you need to also upload files), then the raw data won't even be available. You'll NEED to look at the $_POST array to get that data.
By the way, there's a PHP-specific forum area here that would better answer these types of questions in the future.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.