So I am building an application that would take url from input and pass it to the express via AJAX. Afterwards, the express should validate it and load view. The best case would be to load view in the same url: /share
, but since I didn't succeed in making it that way, I'm trying to load view in the /success
url. Hopefully all is clear until now.
This is the part of my app.js:
//Routers
app.get('/share', ensureAuthenticated, function(req, res) {
res.render('share', {user: req.user});
});
app.get('/success', function(req, res) {
res.render('success', {data: req.url});
});
//Validate
app.post('/share', function(req, res) {
var status = Object.keys(req.body).join('');
var twitter = Dialog.twitter(status);
var url = {url: twitter.get()};
console.log(url);
// res.set('Content-Type', 'application/json');
res.json({data: url});
res.end();
});
AJAX:
function postLinks() {
var shareVal = document.querySelector('#share-input').value;
var xhr = new XMLHttpRequest();
var url = "http://127.0.0.1:3000/share";
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if(this.readyState === 4) {
console.log("Passed: " + this.status);
}
else {
console.log("Error: " + this.readyState);
}
};
xhr.send(shareVal);
console.log(status);
}
My 'view engine'
is 'ejs'
.
When I check the console, I get the object. For example if I've pasted a link http://google.com and submitted it via AJAX, when I check net Network tab I get:
{"data":{"url":"https://twitter.com/intent/tweet?url=http%3A%2F%2Fgoogle.com.com"}}
which is fine, but I don't know how to render it in same, or another page (/success
). The ultimate goal is to share this link via Twitter,
via tholo
No comments:
Post a Comment