0

I am developing a RESTful web service using NetBeans, GlassFish server and MySQL as the backend. I want to create a RESTful web service client using JavaScript which will consume all services through it. I already have created a client that implements the GET, POST and DELETE methods. However, I'd like to implement the PUT method in JavaScript.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Ajit
  • 957
  • 1
  • 8
  • 24

1 Answers1

1

It's pretty much a matter of changing type specification on the client side - but you may have to write some client or server-side logic (e.g. upper-casing or lower casing before evaluation, as part of your input sanitizing), depending on your support parameters. See the link at the end for more details.


With jQuery:

$.ajax({
    url: restfulPutUrl,
    type: "PUT"
}).done(function() {
    $(this).addClass("done");
});

see jQuery docs, especially:

Other HTTP request methods, such as PUT and DELETE, can also be used [with the type parameter], but they are not supported by all browsers.


Without:

 function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }

 var xhReq = createXMLHttpRequest();
 xhReq.open("PUT", "restfulPutUrl", true);

see ajaxpatterns.org if needed


PUT is not implemented uniformly, http://annevankesteren.nl/2007/10/http-method-support for more details.

Peter Hanley
  • 1,254
  • 1
  • 11
  • 19