Reputation: 2488
I have the following jQuery ajax code:
<script>
$(function() {
$("button").click(function() {
$.ajax({
url : "signin.html",
type : "post",
cache: false,
data : "userName=" +$("#userName").val()+ '&password='+$("#password").val(),
success : function(data) {
var obj = jQuery.parseJSON(data);
var msg = '<span>' + obj.message + '</span>';
$('#message').html(msg);
},
//EDIT
error: function(){
alert('Error in response...');
}
});
});
});
</script>
<div id="message"></div>
I have spring controller which sends message as responses which I am trying to print in the message box. I see the request processed by the controller but the message from the controller is not getting displayed in the page. What am I doing wrong here?
public @ResponseBody ModelAndView loginUser(request attributes here){
isUserExisits = service.authenticateUser(userBO);
if (isUserExisits) {
model.addObject("message", "Login Success");
} else {
model.addObject("message", "Login Failed");
}}
EDIT:- I have added error callback after 'success' and error call back function is getting called.
Upvotes: 1
Views: 3276
Reputation: 3676
You are returning a ModelAndView
object and accessing a property which does not exist in ModelAndView
class.If you need just a single message to be displayed change the return type to String
.
public @ResponseBody String loginUser(request attributes here){
isUserExisits = service.authenticateUser(userBO);
if (isUserExisits) {
return "Login Success";
} else {
return "Login Failed";
}}
and On web page you can directly use it.
$(function() {
$("button").click(function() {
$.ajax({
url : "signin.html",
type : "post",
cache: false,
data : "userName=" +$("#userName").val()+ '&password='+$("#password").val(),
success : function(data) {
var msg = '<span>' + data + '</span>';
$('#message').html(msg);
}
});
});
});
Upvotes: 1