royb
royb

Reputation: 713

get data from server with node.js

i want to do an ajax call to node.js with get method that the result of the ajax call will show in the same html page that i call the ajax function

this is the html code:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Sing-in Page</title>
  <link rel="stylesheet" href="css/style.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script>
    $(function() {
      $("#myform").bind('ajax:complete', function() {
             // tasks to do 
             alert('1');
       });    
    });
  </script>
</head>
<body>
  <form method="get" action="http://localhost:8080/Login" id="myform" class="login">
    <p>
      <label for="login">Email:</label>
      <input type="text" name="login" id="login" value="">
    </p>
    <p>
      <label for="password">Password:</label>
      <input type="password" name="password" id="password" value="">
    </p>
    <p class="login-submit">
      <button type="submit" class="login-button">Login</button>
    </p>
    <p class="forgot-password"><a href="index.html">Forgot your password?</a></p>
  </form>
</body>
</html>

the is my node.js code, i know that i need to write something in the res.end function but i don't now what..

var express = require('express');
var app     = express();
var fs = require("fs");
var mysql      = require('mysql');
var pool = mysql.createPool({
    host     : 'XXXX',
    user     : 'XXXX',
    password : 'XXXX',
    database : 'XXXX',
});

app.use(express.bodyParser());

app.get('/Login', function(req, res) {
    pool.getConnection(function(err, connection) {
        var sql = 'select count(id) as ok from users where email=\''+req.body.login+'\' and password=\''+req.body.password+'\';';
        console.log("query: "+sql);
        connection.query( sql, function(err, rows) {
            console.log(rows[0].ok);
            connection.end();
        });
    });
    // what to write here that res.end() will return the result of console.log(rows[0].ok);
    res.end();
});

app.listen(8080, function() {
  console.log('Server running...');
});

i want to get the result of the sql query from the DB

Upvotes: 1

Views: 14564

Answers (4)

Victor Parmar
Victor Parmar

Reputation: 74

You almost had it - just add res.send() after closing the connection :)

app.get('/Login', function(req, res) {
    pool.getConnection(function(err, connection) {
        var sql = 'select count(id) as ok from users where email=\''+req.body.login+'\' and password=\''+req.body.password+'\';';
        console.log("query: "+sql);
        connection.query( sql, function(err, rows) {
            console.log(rows[0].ok);
            connection.end();
            res.send(rows[0].ok); // you simply send your data after you're done your processing
        });
    });

});

On the client side you can bind the success event

$("#myform").bind('ajax:success', function(result) {
    alert(result);
});

Upvotes: 2

danilodeveloper
danilodeveloper

Reputation: 3880

Send the HTTP Get to server:

$.get( "YOUR_URL_HERE/Login", function( data ) {
  alert( data );
});

To return a Json result, you can do something like this:

app.get('/Login', function(req, res) {
    var result;
    pool.getConnection(function(err, connection) {
        var sql = 'select count(id) as ok from users where email=\''+req.body.login+'\' and password=\''+req.body.password+'\';';
        console.log("query: "+sql);
        connection.query( sql, function(err, rows) {
            console.log(rows[0].ok);
            result = rows[0].ok;
            connection.end();
        });
    });
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.json({ status: result })
});

Upvotes: 0

Vivek Sharma
Vivek Sharma

Reputation: 597

You could use something like this:

app.get('/Login', function(req, res) {
          pool.getConnection(function(err, connection) {
        var sql = 'select count(id) as ok from users where email=\''+req.body.login+'\' and password=\''+req.body.password+'\';';
        console.log("query: "+sql);
        connection.query( sql, function(err, rows) {
            console.log(rows[0].ok);
            connection.end();
            var returnData = {};
            returnData.message = rows[0].ok; 
            return res.json(200, returnData);
        });
        });


});

Upvotes: 0

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

If you tryiny to trigger the data event on the server, you have to send data to the server, maybe using Jquery's post method as in:

var postSomething = function(){

    $.post
    (
        "http://127.0.0.1:3000/",
        {wooo:'wooo'},
        function(data)
        {
         console.log(data);
        }
    );
}

when 'data' event, you should write some data in the response object:

req.on('data', function(sock) {
      res.writeHead(200, {"Content-Type": "text/plain"});
      res.end("Hello!!");
});

Upvotes: 0

Related Questions