Delete Using Xhr

32 views
Skip to first unread message

DAZ

unread,
Jul 21, 2011, 11:35:35 AM7/21/11
to RightJS
Hi,

I'm trying to delete some resources using xhr.

html:
<li class="robot">
<img src="robot.png">
<form action="/delete/robot/1" class="destroy"
method="DELETE">
<input type="submit" value="&times;">
</form>
</li>

js:
"form.destroy".onSubmit(function(event) {
this.parent().fade();
event.stop();
this.send();
});

The problem is that it sends the form using a POST request rather than
a DELETE request, so it doesn't work.

Is it possible to do this like this?

cheers,

DAZ

Nikolay Nemshilov

unread,
Jul 21, 2011, 11:39:29 AM7/21/11
to rig...@googlegroups.com
Hi DAZ,

Browsers don't support PUT and DELETE. They can send GET and POST only.

PUT and DELETE are emulated in rails by adding the `_method` parameter to the form. RightJS can do that automatically if you specify the 'method' option. Kinda like that

this.send({
method: 'delete'
});

Or you can add method="post" and <input type="hidden" name="_method" value="delete"/> on your form and then just call `this.send()`


--
Cheers,
Nikolay

> --
> You received this message because you are subscribed to the Google Groups "RightJS" group.
> To post to this group, send email to rig...@googlegroups.com.
> To unsubscribe from this group, send email to rightjs+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rightjs?hl=en.
>

DAZ

unread,
Jul 21, 2011, 11:48:53 AM7/21/11
to RightJS
Thanks Nikolay,

I've now got this in the js:
"form.destroy".onSubmit(function(event) {
this.parent().fade();
event.stop();
this.send({
method: 'delete'
});
});

.... but it is still processing it as a GET request, any reason why?

cheers,

DAZ

DAZ

unread,
Jul 21, 2011, 11:49:21 AM7/21/11
to RightJS
Sorry, it is processing it as a POST request.

DAZ



On Jul 21, 4:39 pm, Nikolay Nemshilov <nemshi...@gmail.com> wrote:

Nikolay Nemshilov

unread,
Jul 21, 2011, 11:55:40 AM7/21/11
to rig...@googlegroups.com
Because, as I said, browsers can't send DELETE requests. They always send a POST request with the _method=delete parameter.

Your server can catch that `_method` parameter and route your request as DELETE in your application. But in reality it is still a POST request

--
Nikolay V. Nemshilov
The Web-Developer

DAZ

unread,
Jul 21, 2011, 11:59:34 AM7/21/11
to RightJS
Sorry, yes I understand what you are saying, but I'm using Sinatra and
have set up a delete request (which works with the non xhr request),
so shouldn't this be being picked up by my app, it isn't deleting,
it's just returning a 404 error.

I hope this is clear.

DAZ
> > For more options, visit this group athttp://groups.google.com/group/rightjs?hl=en.

Nikolay Nemshilov

unread,
Jul 21, 2011, 12:08:44 PM7/21/11
to rig...@googlegroups.com
it is hard to say anything particular without seeing the actual code. and I'm not so sure about Sinatra, don't have much of experience with it deleting things.

Xhr + method: 'delete' - works for sure in Rails, I use it on many projects, the problem must be somewhere else.

Try to make your form to have method="post" and embed the hidden _method field on it. Then try to send the form.

> For more options, visit this group at http://groups.google.com/group/rightjs?hl=en.

DAZ

unread,
Jul 21, 2011, 5:35:25 PM7/21/11
to RightJS
Hi Nikolay,

I'm so embarassed!

The error was in my Sinatra code ... nothing at all to do with Right
JS.

For the record, this works if the form includes a hidden input field
with name='_method' value='delete', it also works if you don't do this
but put this in your JS file:

this.send({
method: 'delete'
});

Sorry for the hassle Nikolay and thanks for your help and patience.

DAZ

Nikolay Nemshilov

unread,
Jul 22, 2011, 12:00:34 AM7/22/11
to rig...@googlegroups.com
Hi DAZ,

No worries. This is what the group for :)

--
Cheers,
Nikolay

Reply all
Reply to author
Forward
0 new messages