Search code examples
javaajaxrestprototypejshttp-put

Is it really possible to call HTTP PUT using prototype


I'm running JEE6 with glassfish v3 on NetBean6.9 and working on RESTful web service.

I have jsp file which contains javascript function below.

It basically read info from HTML input fields and convert to JSON format.

Then with onclick Ajax call, attempt to send JSON string using HTTP PUT method. (i.e. I'm trying to UPDATE the db record using REST)

For js framework I'm using is Prototype1.7

When I test the function below, it always return 404 thus "something went wrong" alert is displayed.

According to my search Prototype above 1.5 version supports HTTP PUT/DELETE methods and to do so add _method to the request URL like what I'm doing:

var url = "/resources/inventory/" + invId + "?_method=PUT";

This will create for instance:

http://localhost:8080/NoJSF/resources/inventory/123?_method=PUT

I looked at Firebug and console showing that the request is actually POST. Not sure but I believe this is because of Prototype using POST tunneling to achieve PUT method?

Also even though Ajax is being called, my Java file with JAX-RS annotated witn @POST is not even being called (@GET version is working with separate data so this is the right file) since the first line of its method that spit message is not showing up so I suspect my Ajax statement has some bug or there is something beyond my thinking.. could anyone give me hint?

function protoAjaxPut() {
            //get all fields value and store into json
            var invId = document.getElementById("invIdField").value;
            var invName = document.getElementById("invNameField").value;
            //put info into JSON format

            var jsonInput = JSON.stringify(new Array(invName));

            var url = "/resources/inventory/" + invId + "?_method=PUT";

            new Ajax.Request(url, {
                method:'put',
                postBody: jsonInput,
                ContentType: 'application/json',
                onSuccess: function(transport) {
                    var responseData = transport.responseText;
                    document.getElementById('putResponseText').innerHTML = responseData;
                },
                onFailure: function() { alert('something went wrong!')}
            })
        }//end protoAjaxPut

Solution

  • They are tunneled:

    http://dobrzanski.net/2007/04/22/using-put-and-delete-methods-in-ajax-requesta-with-prototypejs/