I am trying to use Jquery.ajax() with PUT and DELETE methods and sending some data along with the request but in the php side when printing the request it comes empty.
$.ajax({
url: 'ajax.php',
type: 'DELETE',
data: {some:'some',data:'data'},
}
In PHP:
print_r($_REQUEST);
and I get an empty array.
I don’t know if it is a jquery or php thing or simply I can’t send data that way.
I know DELETE method is meant to be used without sending additional data but here every ajax request is sent to the same script.
To retrieve the contents of the HTTP request body from a PUT request in PHP you can’t use a superglobal like
$_POSTor$_GETor$_REQUESTbecause …No PHP superglobal exists for PUT or DELETE request data
Instead, you need to manually retrieve the request body from
STDINor use thephp://inputstream.$_put = file_get_contents('php://input');The
$_GETsuperglobal will still be populated in the event of a PUT or DELETE request. If you need to access those values, you can do it in the normal fashion. I’m not familiar with how jquery sends along the variables with a DELETE request, so if$_GETisn’t populated you can instead try manually parsing the$_SERVER['QUERY_STRING']variable or using a custom “action” parameter as suggested by @ShankarSangoli to accommodate outdated browsers who can’t use javascript to send a DELETE request in the first place.