1

I have a html page that triggers an ajax request:

$.ajax({
                type: 'POST',
                url: '/usernamecheck',
                data: {"username":username},
                success: function(taken){
                    if(taken === 0){
                        $('#error').text('The username' + username + ' is available!')
                    }else{
                        $('#error').text('The username' + username + ' is not available')
                    }
                },
                dataType: "json"
            })

this is my node.js code:

exports.usernameCheck = function(req,res){
var db;
db = require('./../custom_modules/db.js');
var username = req.body.username;
db.users.find({username:username},function(err,users){

    console.log(username)
    if(users.length === 0){
        //return 0
    }else{
        //return1
    }
})

and I want to respond to this ajax with node.js request but am a little unsure of how to do so?

5
  • 1
    Well the first thing you'd have to do is have a web server written and have it respond to the post request to /usernamecheck. Have you written anything for node yet? I don't think your question is specific enough to really give a good answer. Commented Aug 8, 2012 at 20:49
  • @travis I have, i'll update my question Commented Aug 8, 2012 at 20:54
  • What your AJAX is doing is just making a normal HTTP request via Javascript. If you have a web server set up, you just respond as you would if someone entered the URL into their browser (albeit with json or something). Commented Aug 8, 2012 at 21:02
  • @jli could you give me an example of code to do this? I use express and render html pages using res.render() what is the equivelent for sending JSON? Commented Aug 8, 2012 at 21:08
  • res.send() can take objects and will JSON.stringify them, too, now. Commented Aug 8, 2012 at 22:36

1 Answer 1

3

The jQuery ajax dataType represents the data your're expecting back from the server. I would leave this as json and just change other parts of your code to accommodate the data type.

Client

$.ajax({
  type: 'POST',
  url: '/usernamecheck',
  data: {"username":username},
  success: function(response){
    if( response.taken === true){
      $('#error').text('The username' + username + ' is available!')
    }else{
      $('#error').text('The username' + username + ' is not available')
    }
  },
  dataType: "json"
})

Node

db.users.find({username:username},function(err,users){
  res.json({taken: users.length !== 0})
})
Sign up to request clarification or add additional context in comments.

1 Comment

Although since he's now mentioned he's using express: res.json() rather than res.write(JSON.stringify()). Also, could do taken: users.length !== 0 to cut out that branching.

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.