Thursday, 3 October 2013

403 Forbidden Error on Spring Ajax GET Request

403 Forbidden Error on Spring Ajax GET Request

Well basically just trying to implement some ajax into my spring web
application. For testing purposes I have tried writing some code just to
retrieve 'user' information based on their personal 'id' values when they
press a link/button. I am assuming that it is a Server side error, that
something is wrong with my controller, although I am not entirely sure and
need help getting this to work. This is my current JSP page just for
testing:
<c:forEach var="user" items="${users}">
<tr>
<td><c:out value="${user.id}" /></td>
<td><c:out value="${user.name}"/></td>
<td><c:out value="${user.username}"/></td>
<td><c:out value="${user.email}"/></td>
<td><c:out value="${user.dob}"/></td>
<td><c:out value="${user.authority}"/></td>
<td>
<a class="update" href="<c:url value="/viewUser"><c:param
name="id"
value="${user.id}"/></c:url>"><button>Update</button></a>
</td>
<td>
<a class="delete" href="<c:url
value="/deleteUser"><c:param name="id"
value="${user.id}"/></c:url>"><button>Delete</button></a>
</td>
<td>
<a class="ajax" href="<c:url value="/ajax"><c:param
name="id" value="${user.id}"/></c:url>">Ajax</a>
</td>
</tr>
</c:forEach>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('.ajax').click(function(e){
e.preventDefault();
$.ajax({
url:"http://localhost:8080/SDP_v1.7/ajax",
type: 'GET',
dataType:'json',
contentType: 'application/json',
mimeType: 'application/json',
succes: function(user){
alert(user.id + " + " + user.username);
},
error:function(user,status,er) {
alert("error: "+user+" status: "+status+" er:"+er);
}
});
});
});
</script>
This is my Controller class:
@RequestMapping("/viewUser")
public String updateUser(Model model, @RequestParam(value = "id", required
= false) Integer id) {
User user = usersService.getUser(id);
model.addAttribute("user", user);
return "settings";
}
@RequestMapping(value = "/ajax", method = RequestMethod.GET)
public @ResponseBody User getUser(@PathVariable Integer id) {
return usersService.getUser(id);
}
Essentially, I aiming to loading each user's information into a pop-up
modal with a form. Although I have to get this step working first. Thanks

No comments:

Post a Comment