Here is my service call
<script>
function secureClicked() {
var uname = document.getElementById('uname').value;
var pwd = document.getElementById('pwd').value;
var obj = new Object();
obj.username = uname;
obj.password = pwd;
var userObj = JSON.stringify(obj);
xhr = new XMLHttpRequest();
var url = "http://127.0.0.1:9923/login";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
var json = JSON.parse(xhr.responseText);
document.write(json.accesstoken);
onComplete(xmlhttp.responseText);
}
};
xhr.send(userObj);
}
Here is my backend code
module.exports = {
login: function(req, res) {
MongoClient.connect(mongoUrl, function (err, db) {
if (err) {
console.log('into error of mongo');
res.status(400).json({
message: 'Connection to database failed !!',
error: err
});
}
insertDocument(db, req.body, function (result) {
console.log('into insert doc');
res.setHeader('Content-Type', 'application/json');
res.status(200).json({
access_token: result._id
});
});
});
var insertDocument = function (db, data, callback) {
console.log('into insert doc function');
console.log(data);
var string = JSON.stringify(data);
var objectValue = JSON.parse(string);
var username = objectValue['username'];
var password = objectValue['password'];
console.log(username);
db.collection('user_list').findOne({$and:[{ 'username':username},{'password':password}]}, function(err, result) {
if (err) {
res.status(500).json({
message: 'Failed to add in DB!!'
});
} else {
callback(result);
}
});
}
}
};
I'm able to get response in postman, but unable to get response in browser. Am I doing anything worng here ? Can anyone help me on this? Thanks in advance !!
via Avinash Ravilla
No comments:
Post a Comment