0

I have two IF in my php api that checks if request method is POST or DELTE . For Post method I get the variables in "Data" part of ajax using $_POST. But for DELETE Method I can't do.I've used the code below

$deleteAcKey = file_get_contents("php://input");
$test = array();
parse_str($deleteAcKey, $test);
echo json_encode($test);

However I get Some unreadable data. I want some json Data that i would access it in Ajax By using result.message

Any Suggestion? Any Other way to use?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Hamed Khanezar
  • 91
  • 2
  • 12
  • While not expressly forbidden, it's not expected that DELETE requests will have a body: https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request. If you need to send data in a DELETE request, put it in the querystring. – Rory McCrossan May 14 '19 at 10:19
  • is it common to send data in header of ajax? what is the common way for sending some data to understand which id must be deleted? – Hamed Khanezar May 14 '19 at 10:39
  • Sure, headers would work, as would the querystring as I previously mentioned – Rory McCrossan May 14 '19 at 10:46

1 Answers1

0

There is a difference between POST request and DELETE request. The first one has BODY payload - the second one does not or at least is not considered as standard.

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5

You can send a BODY with your DELETE method but it will be 99% ignored.

In your case you should user query parameters.

Community
  • 1
  • 1
Dilyan Trayanov
  • 549
  • 3
  • 21