Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Play 2: get reply from server to AJAX

I have been using AJAX with Play 2 framework to send a request and apply something at server side.

Play 2.x: How to make an AJAX request with a common button

Jquery and play framework 2 javascript router issue

But now I want to make request to server, check something in database and get reply back to AJAX and redraw image or change text.

What steps should I take to approach it?

Now I have:

controller

public static Result delete(Long id) {
    //...
    return ok();
}

view

<script type="text/javascript">
    $("#delete").click(function() {
        var id = $(this).attr("data-id");
        alert(id);
        jsRoutes.controllers.Items.delete(id).ajax({});
        return false;
    });
</script>

Upvotes: 0

Views: 106

Answers (1)

grotrianster
grotrianster

Reputation: 2468

first you need to send Json response back to browser have a look at official docs, second you have to handle response in javascript like this

jsRoutes.controllers.Items.delete(id).ajax({
  success: function(datafromserver) {
   // if success put your logic here        
 },
 error:function(xhr, status, error) {
    // handle exception
 }
});

Upvotes: 1

Related Questions