在jsp中将数据加载到div内容失败

时间:2016-05-20 03:50:05

标签: jquery html jsp

我有跟随代码,假设在div中显示加载的数据,id = bodyContainer但是重定向到url ...     

function viewCategory() {

$.get('/management/Category', function(data){ //received data 

$('#bodyContainer').html(data); //doesnt show in bodycontainer instead redirects to get method url

}); }

</script>

    <body>
<table class="table">

<tr>
<td><a href="${pageContext.request.contextPath}/assetCategory" onclick="viewCategory()"> View Category</a>
</td>
</tr>
</table>

<div class="col-sm-10 col-md-10 col-lg-10" id="bodyContainer" >Welcome </div>

1 个答案:

答案 0 :(得分:0)

问题是您为href标记指定了a,它显然会重定向。如果您希望ajax在这里,您应该阻止a的默认操作,或者只删除href值并替换为#或{{1} }。

案例1

javascript:void(0)

为此

绑定<a href="${pageContext.request.contextPath}/assetCategory" class="getData"> View Category</a>
Event click

案例2

$('a.getData').on('click',function(e){
    e.preventDefault(); //will prevent from redirecting
    //Also you can use `$.load` instead of `get`
    $('#bodyContainer').load('/management/Category', function(data){ 
          //Anything to execute after load is complete
    }); 
});

您可以使用<a href="javascript:void(0);" onclick="viewCategory()"> 或删除function并将其与onlick绑定,如案例1

所示
jquery