Reputation: 415
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);
});
});
Upvotes: 2
Views: 5262
Reputation: 11065
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
Upvotes: 3