0

I've searched for username availability in google. There was no any good solution to this problem. Here I'm trying to check username availability in database table user. I have following code

<input type="text" name="username" id="usernamee" tabindex="1"class="form-control" placeholder="Username"><span class="status">

this is my script code

<script>
$(function(){
    $("#usernamee").blur(function(){
        var uname = $('#usernamee').val();
        if(uname.length >= 3){
            $(".status").html("<font> Checking availability...</font>");
             $.ajax({
                type: "GET",
                url: "/exist/"+uname,
                success: function(msg){

                    $(".status").html(msg);

                    }
                });
            }
        else{

            $(".status").html("<font color=red>Username should be <b>3</b> character long.</font>");
        }

    });
 });
</script>

this is my controller code

@RequestMapping(value = { "/exist/{name}" }, method = RequestMethod.POST)
public void checkUsername(@PathVariable("name") String username, HttpServletResponse response , HttpServletRequest request) throws IOException {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

        String connectionURL = "jdbc:mysql://localhost:81/name"; // students is my database name
        Connection connection = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = (Connection) DriverManager.getConnection(connectionURL, "root", "root");
        PreparedStatement ps = (PreparedStatement) connection.prepareStatement("select username from users where username=?");
        ps.setString(1,username);
        ResultSet rs = ps.executeQuery();

        if (!rs.next()) {
            out.println("<font color=green><b>"+username+"</b> is avaliable");
        }
        else{
        out.println("<font color=red><b>"+username+"</b> is already in use</font>");
        }
        out.println();



    } catch (Exception ex) {

        out.println("Error ->" + ex.getMessage());

    } finally {
        out.close();
    }

}

When I run these code the statusspan tag only shows checking availability..... only. My controller haven't been invoked. Above code is copied from internet. Any suggestions are welcomed. Thank you in advance

6
  • What do you expect? What is a good solution to you? What is your problem? Commented Jun 26, 2017 at 11:08
  • above code is not working as expected to work I want to show either username is available or not Commented Jun 26, 2017 at 11:11
  • I've edited my question a little bit about error Commented Jun 26, 2017 at 11:32
  • Check the response with error callback. Commented Jun 26, 2017 at 11:47
  • Problem was in spring security configuration class Commented Jun 26, 2017 at 13:46

2 Answers 2

1

You send GET request

 $.ajax({
            type: "GET"

But your controller expect POST

@RequestMapping(value = { "/exist/{name}" }, method = RequestMethod.POST)

Either change ajax to POST or controller to GET

Sign up to request clarification or add additional context in comments.

4 Comments

Not working? Any errors? Add them to the question? Any effects (wrng text etc)? Is the controller invoked? Add some logs to the controller and check them.
There is no error callback so you don't process error response from the ajax. Something wrong but you don't even get the wrong result. Neither can I. Add error callback and check what is the error
I think controller isn't been invoked I've checked with log
@StanislavL is right, request types are different. @"Sangam Jung Gauli" , open a developer tools window (or Inspect element) and click on the Network tab. It will show you all requests you page generates. Type some text into your input and observe that network panel. When you click on http request it will show extra info like request type (GET/POST/...), params and the server response. It might give you some clue to what is going on. If not - please add a screenshot of devtools network tab to your post.
0

I haven't get you question from what you said above, but you should add @ResponseBody for ajax request. I recall what I said above, PrintWriter is used and @ResponseBody is not needed, but use @ResponseBody will be better

1 Comment

add a breakpoint for checkUsername mehotd, does the breankpoint make sense?

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.