Reputation: 2052
I'm trying to build an ajax request that will eventually allow users to sort a list by clicking buttons next to each item. Here's what I have so far:
upLinks.on('click', function(e) {
var link;
e.preventDefault();
link = $(this)[0];
console.log(link.pathname);
$.ajax({
type: 'PUT',
url: link.pathname
});
return false;
});
console.log(link.pathname)
logs out /projects/11/project_items/104/sort
to the console, as expected. However, the ajax request returns an error:
PUT http://localhost:3000/projects/11 400 (Bad Request)
Looking at the server logs, it is in fact going to /projects/11
, not /projects/11/project_items/104/sort
for some reason. According to the jQuery docs, /projects/11
would be the default parameter for ajax in this case, because that's the route of the current page. Which leads me to believe it's ignoring the url parameter altogether.
If I change HTTP method type to GET
, the ajax runs as expected. Is there some reason /projects/11/project_items/104/sort
is not an acceptable url for a PUT
request? I thought that because I'm updating data (with the new position) a PUT
request would be the most logical option.
Upvotes: 4
Views: 355
Reputation: 8206
Just a note, if you're using an IIS webserver and the jquery PUT or DELETE requests are returning 404 errors, you will need to enable these verbs in IIS. I've found this to be a good resource: http://geekswithblogs.net/michelotti/archive/2011/05/28/resolve-404-in-iis-express-for-put-and-delete-verbs.aspx
"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." from: http://api.jquery.com/jQuery.ajax/#options
Reference stack question: How to send a PUT/DELETE request in jQuery?
Upvotes: 3