1

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.

1 Answer 1

1

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);
                }
            });
        });
    });
Sign up to request clarification or add additional context in comments.

4 Comments

i have changed my controller, but stil not working. i have alert in success call back which is not working !!!!.
Does controller code executed successfully? IF yes than what is the error message on browser web console?
yes the controller code gets executed, and my error callback function is getting called, 'Error in response...'
put a try catch in your controller code and post the stacktrace I am sure there's an exception in service.authenticateUser(userBO) call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.