5

I am trying to make a PUT request to a RESTful web service, however, it appears that jQuery 1.5 does respond to any changes in the 'type' setting. The request is sent as a GET no matter the value in 'type'. In jQuery 1.4 this isn't a problem.

Here's my code:

$.ajax({
    type: "PUT",
    url: "https://api.somesite.com/v1.0/people/" + individualID + "/",
    dataType: "jsonp",
    data: $("#editProfile").serializeArray(),
    cache: "false",
    success: function(data,textStatus,jqXHR) {
        $.modal.close();
    },
    error: function(jqXHR,textStatus,errorThrown) {
        alert("Error!");
    }
});
Austin
  • 291
  • 1
  • 4
  • 12
  • 1
    You haven't actually asked a question so much as made a bug report. – Phrogz Feb 05 '11 at 19:33
  • Tried in both Safari and Firefox – Austin Feb 05 '11 at 19:35
  • 1
    You can't make a JSONP request by anything other than `GET` because it's not an AJAX request at all. AJAX requests can only be made to your own domain. JSONP fakes it by creating ` – Dan Grossman Feb 05 '11 at 19:40

4 Answers4

6

As far as I'm aware, you can't make a JSONP request via PUT. Since JSONP works by injecting a <script> element pointing to the remote domain, that request will always be a GET request.

If you absolutely must make a PUT request to a remote domain, you'll need to either use a server-side proxy on your local domain or look into CORS if you don't need IE support.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • When I removed the "dataType: jsonp" setting from the request, the browser did in fact make a PUT request to the remote domain. – Austin Feb 05 '11 at 20:03
  • It's using CORS. CORS on IE is largely broken and I wouldn't be surprised if @Dave is right and you cannot change the method. – Michael Lorton Feb 06 '11 at 12:24
0

From the jQuery.ajax() docs:

The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Perhaps with some additional browser info we can figure out what is causing the problem, but for now it seems jQuery does not want to guarantee functionality except on GET and POST. This is surprising for me to find out =)

babtek
  • 934
  • 5
  • 6
0

How do I PUT data to Rails using JQuery maybe?

edit: oups, you didnt say the webservice was in Rails. But it might support something like that too. Did you try just sending a POST request?

Community
  • 1
  • 1
Robin
  • 21,667
  • 10
  • 62
  • 85
0

I was struggling with something similar. I have been able to send a PUT successfully pre 1.5 but stopped working with 1.5. I know there was a big change to how the ajax stuff in handled in 1.5 so I'll look into that next. When it did work it worked fine for me in safari, firefox & chrome. When it works you'll first get an OPTIONS being sent and as pointed out before your server side will have to response satisfactorily to the OPTIONS request a la CORS. Here is a piece ot test code that does work for me pre 1.5 so it is possible. As an aside I was not able to get firefox to cache the OPTIONS response client side. The other browsers did.

http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" 

var url = 'http://api.example.com/rest/action?key=123ABC&data={"value":55}';
$.ajax({
  type: "PUT",
  url: url,
  data: {},
  success: function(msg){
    alert( "Data Saved: " + msg );
  },
  error: function(msg){
     console.debug(msg);
  }
});
David Kierans
  • 1,599
  • 1
  • 16
  • 24