2

I am using javascript,html,nodejs,express and mysql to retrieve values from database and pass it back to html .

Here , the user enters a domain in the 'btnSearch' text box and presses Load button . The javascriot calls the /getallusers code in app.js . This function should access the database and return the result .

The problem i am facing is : i am not able to pass on the value in 'btnSearch' to the app.js . If i include the tags in html , then the request does not go to /getallusers .instead it goes to /hi.html?input=Music . So i have not used the tag .

The html page is as below :
var xmlDoc = null ;

function load() {
var str = document.getElementById('btnSearch');
alert(str);
if (typeof window.ActiveXObject != 'undefined' ) {
  xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
  xmlDoc.onreadystatechange = process ;
}
else {
  xmlDoc = new XMLHttpRequest();
  xmlDoc.onload = process ;
}
xmlDoc.open( "GET", "/getallusers?input="+str, true );
xmlDoc.send( null );
}

function process() {
if ( xmlDoc.readyState != 4 ) return ;
document.getElementById("output").value = xmlDoc.responseText ;
}
}</script><body>
<input type="text" name="input" id="btnSearch" />
<textarea id="output" cols='70' rows='40'><empty></textarea>
<br></br>
<button onclick="load()">Load</button> 
<button onclick="empty()">Clear</button>
</body>
</html>

My express function is :

    app.get('/getallusers', function (req, res) {
    var input_domain=req.query.input;
    console.log(input_domain);
    connection.query('SELECT DBName FROM CrawlerDBInfo where Domain ='+"'"+input_domain+"'"+';', function (error, rows, fields) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        str = '';
        for (i = 0; i < rows.length; i++)
        str = str + rows[i].DBName + '\n';
        res.end(str);
    });
});

1 Answer 1

3

you can accept data using routes in node.js app as

app.get('/getallusers/:input', function (req, res) {
    var input_domain=req.params.input;
    console.log(input_domain);
    connection.query('SELECT DBName FROM CrawlerDBInfo where Domain ='+"'"+input_domain+"'"+';', function (error, rows, fields) {
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
        str = '';
        for (i = 0; i < rows.length; i++)
        str = str + rows[i].DBName + '\n';
        res.end(str);
    });
});

by using ":" in your routes you can specify parameter. And from your javascript in UI you send get call with the parameter as

localhost:3003/getallusers/inputdomainvalue

SO the above route will be executed. For more information see api

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

2 Comments

i tried this and i am getting the error : Cannot GET /127.0.0.1:1213/getallusers/Music . i have a doubt. do we need to specify the get call as the following ? localhost:3003/getallusers/?input=domainvalue
You need to add a route in your application as app.get('/getallusers/:input', function (req, res) {var input_domain=req.params.input;//here you will get value music});

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.