UPDATE: Tried debugging using an absolutely wrong method. I was double-clicking the .php file through Network tabs and assumed that it was sending the same headers as it would through the ajax POST request. That's why GET was working for me (because the data is packaged within the URL, double clicking the php file meant I was simulating exactly what would happen in the ajax request).
Thanks to @Phil for pointing me in the right direction: After using error to print back the resulting data, I found that the responseText included what I originally wanted from the POST request.
Below this point is the original post
I am trying to send 2 pieces of information through a POST request without using a form.
It seems to be sending the data properly as ''Form Data'' when I look at the headers for the php file in Chrome's Network tab. However, using print_r($_POST)
to check if the data is being retrieved on the PHP side results in just an empty array : "Array ( )"
JS
url = "/members/duc.php";
data = {
"d": "test",
"r": 156
};
$.ajax({
'type': "POST",
'async': false,
'global': false,
'url': url,
'data': data,
'dataType': "json",
'success': function (data) {
jsonContainer = data;
console.log('success');
}
});
PHP
print_r($_POST);
Attempts at debugging
5. Changing ajax method: 'POST'
to method: 'GET'
showed the data on the server side with the code print_r($_GET)
.
1. I have tried $_REQUEST
and also setting an arbitrary var = file_get_contents("php://input");
2. I have tried sending as json then decoding input.
3. I have tried using $.post
instead of $.ajax
.
4. I have tried setting the data in ajax to {duc:data}
6. Tried the POST method with www. included in the address bar, no change.
$_GET was working because