Skip to content

Instantly share code, notes, and snippets.

@dataserver
Last active November 29, 2019 01:52
Show Gist options
  • Save dataserver/a697ab647be61221eccff76e9defdf62 to your computer and use it in GitHub Desktop.
Save dataserver/a697ab647be61221eccff76e9defdf62 to your computer and use it in GitHub Desktop.
Use ajax POST method with X-HTTP-Method-Override to workaround when you can't use PUT/DELETE/XXX
$.ajax({
type: 'POST', // Use POST with X-HTTP-Method-Override
dataType: 'json', // Set datatype - affects Accept header
url: "http://example.com/people/1", // A valid URL
headers: {"X-HTTP-Method-Override": "PUT"}, // X-HTTP-Method-Override set to XXX.
data: {"name": "John Doe"}
});
/*
Some clients do not support PUT or other method beyond GET/POST.
When I was trying to implement a REST server on phpDesktop,
the Mongoose server had problems accepting PUT, DELETE, PATCH
methods.
For these cases, you could use POST with a request header of
X-HTTP-Method-Override set to PUT/DELETE/PATCH.
What this tells the server is that the intended request is a XXX.
PHP, check for $_SERVER['X_HTTP_METHOD_OVERRIDE']
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment