AJAX DELETE not working

216 views
Skip to first unread message

chris

unread,
Jun 27, 2011, 5:10:21 AM6/27/11
to play-framework
Hello,

I've got a problem with ajax DELETE : i want to call a controller's
delete action => i use jQuery .ajax from my form :

<p>
<a href="" id="delete">Delete</a>
</p>

$("#delete").bind("click", function ()
{
var listAction = #{jsAction @delete(':id') /};
$.ajax({
url:listAction({id: '${lot?.id}'}),
type:'DELETE',
error: function(msg) {alert(msg)}})
}

The sent url is correct and triggers the controller's method below
(and the deletion)

public static void delete(Long id) {
if (id != null) {
Lot lot = Lot.findById(id);
lot.delete();
index();
}

BUT it returns an error and trigger a GET operation on the same URL
instead of going back to index page...

Dominik Dorn

unread,
Jun 27, 2011, 11:21:50 AM6/27/11
to play-fr...@googlegroups.com
three things:

1. why are you redirecting the user's client for an ajax call? I always return a simple ok(); or a renderJson()
2. you haven't checked if lot is null or not
3. look at your console... the exception should be logged there



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




--
Dominik Dorn
http://dominikdorn.com
http://twitter.com/domdorn

Skripten, Mitschriften, Lernunterlagen, etc. findest Du auf http://www.studyguru.eu !

chris

unread,
Jun 27, 2011, 12:40:56 PM6/27/11
to play-framework
Hi,

1. because after the delete the page has no interest from an end user
perspective. I also tried to return ok and make a redirect into the
javascript success code but i still go into the error "branch"
2. the test is done (i did not copy it), in fact the delete link does
not show up if lot is null
3. nothing appears in the console that's why i finally posted this
case


I could take the second way which is using a form POST with x-http-
method-override=DELETE but i don't like not to understand what's going
wrong...

canavar

unread,
Jun 30, 2011, 3:32:27 AM6/30/11
to play-fr...@googlegroups.com

Hi, this video[1] might help you. Although it is in Turkish, it provides code samples for your problem.

[1]: http://fehmicans.net/blog/2011/06/18/play-framework-ve-jquery-ile-ajax-cagrilari-kullanarak-crud-islemleri/

27 Haz 2011 19:41 tarihinde "chris" <chris....@gmail.com> yazdı:
Reply all
Reply to author
Forward
0 new messages