Allow PUT, DELETE and other HTTP methods on ajax requests
Reported by Pablo Bustos | August 18th, 2008 @ 10:58 PM
"request" function of Ajax.Request class, simulate other verbs (e.g. DELETE, PUT, HEAD, etc) over post.
Why restrict to only get and post requests?
To enable prototype use servlets of my project (that requires DELETE and PUT methods), as a patch, I replaced in that function this line:
this.transport.open(this.method.toUpperCase(), this.url,
this.options.asynchronous);
by this:
this.transport.open(this.options.method.toUpperCase(), this.url,
this.options.asynchronous);
and works perfectly.
Comments and changes to this ticket
-
John-David Dalton August 18th, 2008 @ 11:43 PM
- → State changed from new to invalid
As far as I know they switched from explicitly setting the http method and instead set a "_method" param
read: Common Options, under "method" http://www.prototypejs.org/api/ajax/options
-

Pablo Bustos August 19th, 2008 @ 04:50 AM
I could not find the answer in that reference, why restrict to only GET and POST?
Do you know if the _method param corresponds to a w3c standard?
How can I manage a _method param from a java servlet? Servlets cannot process post and get params concurrently...
-
John-David Dalton August 19th, 2008 @ 04:53 AM
- → State changed from invalid to new
- → Assigned user changed from to Tobie Langel
Tobie or Sam might be able to explain this more.
-
Tobie Langel August 21st, 2008 @ 12:46 AM
DELETE and PUT request are not implemented in all supported browsers.
-
Tobie Langel August 21st, 2008 @ 12:46 AM
- → Tag changed from to ajax needs_docs needs_patch needs_tests
- → State changed from new to enhancement
-

Pablo Bustos August 21st, 2008 @ 03:34 AM
I'd like to access a REST WS using ajax, and, as a rest service, all resources are accessed with a generic interface (e.g., HTTP GET, POST, PUT, DELETE).
-
Tobie Langel August 22nd, 2008 @ 02:14 AM
Sure, only, as I mentioned, using DELETE and PUT isn't possible in some of the supported browsers, hence the current work-around.
Determining which current browsers support what warrants tests, so those would be welcomed.
-
Mark Caudill August 24th, 2008 @ 08:39 PM
Here is a good starting point for this: http://annevankesteren.nl/2007/1...
Firefox “supports” the methods from RFC 2616 and RFC 2518, except for TRACE. That method throws an exception for security reasons. All the supported HTTP methods are case-insensitively matched against and uppercased prior to the request.
Opera has an issue with OPTIONS: the request never finishes loading. (In earlier versions of Opera it is converted to GET.) For known methods it performs a case-insensitive match just like Firefox. The known methods are GET, HEAD, POST, PUT, and DELETE. HEAD and PUT never have a response entity body. All other methods, including custom, result in GET requests.
IE has probably the weirdest implementation. It performs a case-insensitive match against a list of supported methods, just like the other browsers. However, only for GET, POST, and PUT is the input actually uppercased before the request. So get results in GET, but head results in head. In this way, it supports RFC 2616 and RFC 2518 with TRACE and CONNECT as exceptions. If you use HEAD (uppercase) there will not be any response body. Methods not supported by IE will cause an exception to be raised. (This could be different for prior versions of IE.)
-

David Burger September 5th, 2008 @ 05:01 AM
On line 83 of ajax.js which currently looks like this:
if (!['get', 'post'].include(this.method)) {Why not allow 'head' requests? I understand the problem with 'put' and 'delete' may be that most browsers don't support them. Surely most browsers support head.
-
Mark Caudill September 5th, 2008 @ 05:55 AM
David: Opera doesn't interpret HEAD correctly at all. IE does have problems, but I'm not sure how much that would affect the actual response by the server (with a lowercase 'head' request).
-
John-David Dalton September 5th, 2008 @ 07:32 AM
@Mark - I think you misread the article you linked to.
http://www.ietf.org/rfc/rfc2616
9.4 HEAD The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The meta-information contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining meta-information about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.HEAD doesn't return any responseText, I have tested this with IE, Firefox, Opera 9.25/9.5 and Safari 3.1. None return responseText and none error.
The issue with "head" lowercase and IE is addressed in Prototype via this.method.toUpperCase() which forces the method to be declared in Upper case, avoiding the browser's case conversions.
I am not sure why Prototype makes methods other than GET or POST into a _method param. Ruby on Rails seems to support other methods: http://dev.rubyonrails.org/changeset/5621
If anything I think an option to 'forceMethod' should be allowed. (+1 Mark) If we change how default options are set:
Ajax.Base = Class.create({ initialize: function(options) { this.options = Object.clone(Ajax.Base.defaultOptions); Object.extend(this.options, options || { }); this.options.method = this.options.method.toLowerCase(); if (Object.isString(this.options.parameters)) this.options.parameters = this.options.parameters.toQueryParams(); else if (Object.isHash(this.options.parameters)) this.options.parameters = this.options.parameters.toObject(); } }); Ajax.Base.defaultOptions = { method: 'post', forceMethod: false, asynchronous: true, contentType: 'application/x-www-form-urlencoded', encoding: 'UTF-8', parameters: '', evalJSON: true, evalJS: true };Then users could make all requests
forceMethodbyAjax.Base.defaultOptions.forceMethod = true
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
The Prototype JavaScript library.
