1

I have a program which should send a JSON from the Console to a PHP

var nice = any_jsonfile;
console.log(JSON.stringify(nice)); // here it shows desired contents
var nice2 = JSON.stringify(nice);
        $.ajax({
                type: "POST",
                url: "/postgetter.php",
                data: nice2,
                dataType: "text"
        });

The Postgetter contains

<?php


$data = $_POST;

$data_string=implode ( $data );


 $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
 $txt = $data_string;
 echo "<pre>";
 print_r($data);
 echo "</pre>";
 fwrite($myfile, $txt);
 fclose($myfile);

 ?>

I see the ajax is sending it but it doesn't get any data in to the postgetter.php

What I am doing wrong here?

theode
  • 300
  • 1
  • 7
  • I tried this but also not working http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript?rq=1 – theode Feb 02 '15 at 10:24
  • Is `postgetter.php` in the same directory? If so use `postgetter.php` and not `/postgetter.php`. The `/` might be sending it back to the root folder and depending on how your files are set up, that may not be what you want. – Albzi Feb 02 '15 at 10:31
  • postgetter.php is in the root, thats right – theode Feb 02 '15 at 10:38
  • What happens if you do `die(var_dump($_POST));` on `postgetter.php`? – Albzi Feb 02 '15 at 10:53
  • undefined=&undefined=&undefined=&undefined= – theode Feb 02 '15 at 11:10

1 Answers1

0

try it with:

$data = file_get_contents("php://input");

see https://stackoverflow.com/a/12997460/2460773

sometimes $_POST global will be empty, depending on the type of data sent. in that case you could use file_get_contents to get the raw POST data.

Community
  • 1
  • 1
Nitsan Baleli
  • 5,393
  • 3
  • 30
  • 52