haidousm
haidousm

Reputation: 555

JQUERY AJAX $_POST is empty

I have two files, a javascript file, and a PHP file.

In the JS file, i'm running a POST request.

$.ajax({
    type: "POST",
    url: 'http://localhost/tracker/functions.php',
    dataType: 'text',
    data: {
        name: "John Doe",
        age: "19"
    },
    success: function (obj, textstatus) {
        console.log(textstatus);
    },
    error: function (xhr, status, error) {
        console.log(xhr, status, error);
    }
});

and I'm simply print $_POST in the php file

<?php
   print_r($_POST);
?>

which just prints an empty array

Upvotes: 0

Views: 70

Answers (1)

Flo
Flo

Reputation: 356

to get JSON data from my PHP script I usually do

echo file_get_contents('php://input');

So maybe you can do in your php file

<?php
  print_r(file_get_contents('php://input'));
?>

Cf PHP "php://input" vs $_POST

Upvotes: 1

Related Questions