PUT, DELETE data from jQuery to PHP

I am trying to use jQuery.ajax () with the PUT and DELETE methods and send some data along with the request, but on the php side it is empty when printing the request.

$.ajax({ url: 'ajax.php', type: 'DELETE', data: {some:'some',data:'data'}, } 

In PHP:

 print_r($_REQUEST); 

and I get an empty array. I do not know if it is jquery or php, or just I can not send data this way. I know that the DELETE method is intended to be used without sending additional data, but here every ajax request is sent to the same script.

+7
source share
3 answers

To get the contents of the body of an HTTP request from a PUT request in PHP, you cannot use a superglobal like $_POST or $_GET or $_REQUEST , because ...

There is no super global PHP file for PUT or DELETE request data

Instead, you need to manually load the request body from STDIN or use the php://input stream.

$_put = file_get_contents('php://input');

Superglobal $_GET will still populate if a PUT or DELETE request is made. If you need to access these values, you can do it in the usual way. I am not familiar with how jquery sends variables with a DELETE request, so if $_GET not populated, you can instead try manually parsing the variable $_SERVER['QUERY_STRING'] or use the custom action parameter suggested by @ShankarSangoli to host legacy browsers that cannot use javascript to send a DELETE request in the first place.

+16
source

I think DELETE type is not supported by all browser. I would add an additional say action: delete parameter. Try it.

 $.ajax({ url: 'ajax.php', type: 'POST', data: { some:'some', data:'data', action: 'delete' }, } 
+4
source

This may be a browser issue as indicated in the jQuery documentation:

Type of request ("POST" or "GET"), by default - "GET". Note. Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

http://api.jquery.com/jQuery.ajax/

+3
source

All Articles