prototypejs / prototype

Prototype JavaScript framework
http://prototypejs.org/
Other
3.54k stars 639 forks source link

ajax.request to support more http method verbs (put, delete, ...) #268

Closed jwestbrook closed 9 years ago

jwestbrook commented 9 years ago

previous lighthouse ticket #1220 by retep


prototype currently supports get and post verbs in ajax.request. for other methods it does a horrible hack where it adds a _method= param.

Prototype can be a useful tool for implementing REST-ful clients in a user's browser. For wiring up a web page to a server side restful interface.

Restful interfaces can often use verbs other than get and post. e.g. they can use delete and put. quite commonly.

Standard restful interfaces will not often be changeable by the end user (the prototype user). e.g. telling twitter or google to add support for a _method= param in their api instead of requiring a PUT likely will not be successful.

I've made some minor changes to prototype, diff attached, so that it supports the standard verbs (get post put delete head) (trace?) And added support for bodies on puts (as well as posts)

--- rimuhosting/web/js/prototype.js (revision 8777)
+++ rimuhosting/web/js/prototype.js (working copy)
@@ -1506,7 +1506,7 @@
           this.options.parameters :
           Object.toQueryString(this.options.parameters);

-    if (!['get', 'post'].include(this.method)) {
+    if (!['get', 'post', 'put', 'delete', 'head'].include(this.method)) {
       params += (params ? '&' : '') + "_method=" + this.method;
       this.method = 'post';
     }
@@ -1530,7 +1530,7 @@
       this.transport.onreadystatechange = this.onStateChange.bind(this);
       this.setRequestHeaders();

-      this.body = this.method == 'post' ? (this.options.postBody || params) : null;
+      this.body = (this.method == 'post' || this.method == 'put') ? (this.options.postBody || params) : null;
       this.transport.send(this.body);

       /* Force Firefox to handle ready state 4 for synchronous requests */
@@ -1556,7 +1556,7 @@
       'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
     };

-    if (this.method == 'post') {
+    if (this.method == 'post' || this.method=='put') {
-       headers['Content-type'] = this.options.contentType +
+       headers['Content-Type'] = this.options.contentType +
         (this.options.encoding ? '; charset=' + this.options.encoding : '');
savetheclocktower commented 9 years ago

Duplicate of #280.