从动态URL通过jsp调用ajax

时间:2015-07-30 11:18:18

标签: java jquery ajax jsp spring-mvc

jsp file        
<td><a id="delete" href="delete?id=${user.id}">Delete</a></td>
  <div id="result"></div>
ajax call
$('#delete').click(function(){
  $.ajax({
            url : $(event.target).attr("href"),
            success : function(data) {
                $('#result').html(data);
            }
        });

});

spring controller class with delete method
 @RequestMapping(  value = "/delete", method = RequestMethod.GET)
      public @ResponseBody
        String deleteUser(@RequestParam int id) {
          System.out.println("Entered deleteUser");
            dataService.deleteRow(id);
            return "deleted "+id;
      }

这里我试图通过使用jquery ajax调用delete user方法从jsp中删除一条记录,但在删除它到url / delete的新页面后?id = 180(基于记录id的id)而不是它应该删除在同一页面上,如何删除记录并在同一过程中显示所有记录。

2 个答案:

答案 0 :(得分:1)

HTML页面

<div id="data">
</div>
<a class="linkDelete" userid = "1" href="#">Delete</a>

<强>的jQuery

$(document).ready(function(){
    $(".linkDelete").click(function(){
        var userid = $(this).attr("userid");
        var url = "www.example.com?id=" + userid;
        alert(url);

        $.get( "/delete", function( data ) {
              alert( "Data Loaded: " + data );
        });
    });
});

如果您需要https://jsfiddle.net/r47fbt28/

答案 1 :(得分:0)

为什么在进行Ajax调用时,href元素有<a>属性?

<td><a id="delete" href="delete?id=${user.id}">Delete</a></td>

只需将其修改为

即可
<td><a id="delete" href="#" >Delete</a></td>