0

Let's put examples to demonstrate my problem:

index.html:

<!doctype html>

<html>

  <head>

    <title>Testing XHR</title>

    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

  </head>

  <body>

    <script>

      $(function()
      {
        const test = "This is a test text.";
        $.post("test.php", {data: test}, function()
        {
          console.log(test);
        });
      });

    </script>

  </body>

</html>

test.php:

<?php
var_dump($_POST);

So, when I load index.html, it completes $.post() request successfully. I have checked it via Chrome's "Inspect" option, under "Network" tab. But when I load test.php, it shows empty string. I have tested it to my local server and a live server, results are the same. What am I doing wrong?

msrumon
  • 1,250
  • 1
  • 10
  • 27
  • Possible duplicate of [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – Chris White May 12 '19 at 06:24

2 Answers2

0

console log the response you get from test.php.

$.post("test.php", {data: test}, function(response)
{
    console.log(response);
});

loading the test.php won't show you the post data.

LogicalAnt
  • 907
  • 3
  • 12
  • 28
  • Console throws this error: `Access to XMLHttpRequest at 'file:///C:/xampp/htdocs/1498960/test.php' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.` – msrumon May 12 '19 at 06:35
  • are you checking it on remote or local server? – LogicalAnt May 12 '19 at 06:45
  • local server. but i tried it on live server as well, the issue persists. – msrumon May 12 '19 at 06:56
0

say you sent data at 9 am by running html file. It sends data to php. but as php isn't returning response, you don't get that. now you ar running php file at say 9.01am. how will you get data now?

You need to print out response.

try the following.

$(function()
      {
        const test = "This is a test text.";
        $.post("test.php", {data: test}, function(response)
        {
          console.log({test, response});
        });
      });
  • Console throws this error: `Access to XMLHttpRequest at 'file:///C:/xampp/htdocs/1498960/test.php' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.` – msrumon May 12 '19 at 07:06