Paul
Paul

Reputation: 1383

Java Spring jQuery ajax not working

I have the following code

var email = document.getElementById("email").value;
    $.post("/valid",{emailadd: email},function(data){
        alert(data);
    });

on the server I have the following:

@RequestMapping(value = "/valid", method=RequestMethod.POST)
public @ResponseBody Boolean checkValidEmail(@RequestParam("emailadd") String emailadd){
    return false;
}

Using firebug I can see that var email get the value but it skips past the alert function, what am i doing wrong?

Upvotes: 0

Views: 436

Answers (3)

Felix L
Felix L

Reputation: 246

I don't see anything wrong with your code. Maybe a stupid question but are you sure that the URL is right ? What about the webapp and the controller in the path ?

As a side note : $("#email").val() would be more jQuery friendly to get the email :)

Upvotes: 0

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

i think the problem is that you don't return any response. You are returning false or true, but doesn't this just stops the checkValidEmail method?

Not sure how this works within spring, but cant you render some text as output and see if that's outputted?

Upvotes: 1

Vincent Agnello
Vincent Agnello

Reputation: 475

It looks like to me that the call hasn't completed yet, so data won't be populated. You will need to add a callback handler for complete

    var email = document.getElementById("email").value;
        $.post("/valid",{emailadd: email},
            complete: function(data){
               alert(data);
            }
    });

Upvotes: 0

Related Questions