I am trying to create my own API in PHP.
Calling my API:
$.ajax({
url: 'api.php',
data: {'param': 'test',
'param2': 'test'},
type: 'GET',
success: function(data) {
console.log(data);
}
});
But how can I get the param (test) in my API? I thought it is in $_POST
but its empty.
If you want the data to be available in $_POST
, you need to send the data through POST:
$.ajax({
url: 'api.php',
data: {'param': 'test',
'param2': 'test'},
type: 'POST', // THIS LINE. Can be GET, POST, or PUT
success: function(data) {
console.log(data);
}
});
If type='GET' then your data should be found in $_GET
Use the proper array, if inside the ajax request you set the Type to POST, use $_POST['key']
, if you set it to GET, use $_GET['key']