1

I am writing a simple application which is running on Nodejs. I am using centos. Front end is html and JavaScript. In the HTML page, I have 2 forms. On selection of data from form1 and clicking on a button, a table has to be loaded inside form2. I have managed to do this functionality with JavaScript. (For now I have used hard coded values.) If I were to retrieve values from database, how should I proceed? I am a noob in Nodejs ! I am processing all the requests using similar methods as below:

app.post('/DomainInfo', function (req, res) {
console.log("POST: ");
DOMAIN = req.body.DomainName;
LOCALE = req.body.locale;
connection.query('insert into DomainInterval ( Domain , Locale , ASRFrequency ,     NLUFrequency, Numinstances ) values (' + "'" + DOMAIN + "'" + ',' + "'" + LOCALE + "'" + ",'" + ASRFRQ + "'" + ',' + "'" + NLUFRQ + "'" + ',' + NONODES + ');', function (error, rows, fields) {

    res.writeHead(200, {
        'Content-Type': 'text/plain'
    });
    res.end('record inerted...');
 });
  connection.query('commit;');
  console.log('success!');
});


// Launch server
app.listen(1213);

While calling these methods, the data retrieved form database is displayed on a separate page. I want it to be sent to the same html page from where the request was sent, sort of like an AJAX call. Is there any way to do this without PHP or servlet?

1
  • are you using express? if so pls mention it Commented Nov 21, 2013 at 4:55

1 Answer 1

3

If i were to retrieve values from database , How should i proceed ?

find an appropriate database and figure out the best driver for it. here are some

  1. mongodb : node-mongodb-native.
  2. mysql : node-mysql

Sort of like an AJAX call . Is there any way to do this without php or servlet ?

from your code you seem to use express framework. to make ajax calls, you just need to make a route specific for the ajax target and give the same in your client side ajax request.

its as simple as:

node.js

app.post('/ajaxTarget', function (req, res) {
    //get data from database and return data like
    res.json(data);    
});

client side

//assuming you are using jquery
$.post("/ajaxTarget",function(data){
    //process the data
});
Sign up to request clarification or add additional context in comments.

7 Comments

Hi thanks . I am using node-mysql and express framework. In the client side , I am using javascript and not jquery . Is there any way to receive the ajax result with javascript
@rasikavijay: definitely possible. i just put the jquery example as it was a one liner plus no fear of browser compactibilities. :)
Thanks .One more Doubt . The data being retrieved from database has to be formatted to a json format and then sent ? Can you please send any tutorial link encompassing the above information .?
the data retrieved from query should be a JavaScript object, so you can pass it directly into res.json(yourObj);
Hi. I m still at a loss on how to process the response. I have given my complete problem and code in this link . Please review and suggest link
|

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.