0

I'm implementing a feature on a site run on a local server where it checks actively whether a username is available or not. The problem is that $.get() seems to not work at all and isn't calling the corresponding app.get() function.

script.js:

 $('#username').keyup(function() {
    var username = $('#username').val();


    $.get('/getCheckUsername', {username: username}, function(result) {
        alert('Query done.') //just to test that it has been called
        alert(result);
    })

})

No alerts are showing up on any keystroke on the page.

routes.js:

app.get('/getCheckUsername', signupController.getCheckUsername);

signUpController:

getCheckUsername: function(req, res) {
    var username = req.query.username;

    database.findOne(User, {username: username}, 'username', function(result) {
        res.send(result);
    });

}
SS-Salt
  • 139
  • 7
  • 1
    Check the console in dev tools for any errors. Also check the network tab after making the request to see the state of the response – Rory McCrossan Apr 16 '20 at 12:03
  • Does the `#username` element exist? Is the `keyup` event successfully being bound and handled? When you debug in the browser's debugging tools, what specifically happens? – David Apr 16 '20 at 12:05
  • The keyup is working fine. I tested it by putting an alert call at the start of the function before. – SS-Salt Apr 16 '20 at 12:07
  • @RoryMcCrossan The console says that `"script.js:49 Uncaught TypeError: $.get is not a function"` – SS-Salt Apr 16 '20 at 12:08
  • Did you try with `$.get("http://localhost:port/getCheckUsername")`? – Eye Patch Apr 16 '20 at 12:08
  • 1
    @SS-Salt: Then `$.get` is not a function. Where are you loading jQuery? What version are you using? – David Apr 16 '20 at 12:10
  • 3
    @SS-Salt in that case either `$` does not refer to jQuery, or you're using the 'slim' branch of jQuery and need to change that to the full version – Rory McCrossan Apr 16 '20 at 12:11
  • 1
    @RoryMcCrossan That seems to be the solution. Thank you. Didn't realize that the slim version had no Ajax. – SS-Salt Apr 16 '20 at 12:14

0 Answers0