Jquery GET request and variable
See original GitHub issueHi!
I have a function which get an ejs page (from my ExpressJS server), and this works. But when I want to get the argument, I can’t.
Here is what I have tried :
routes.js
router.get('/', function(req, res, next) {
req.session.lastPage = '/index';
res.render('index', { lastPage: req.session.lastPage } );
});
index.ejs
<html>
...
<div id="ajax-page"></div>
...
<!-- javascript imports -->
</html>
navigation.js
$.get('/'+page)
.done(function(data){
var template = ejs.compile(data);
$('#ajax-page').html( template(data) ); // Add to div
var lastPage = ejs.render('<% if (typeof lastPage !== "undefined") { %> <%= lastPage %> <% } else { %> lastPage <% } %>', data);
// var lastPage = ejs.render('<% if (typeof lastPage !== "undefined") { %> <%= lastPage %> <% } else { %> lastPage <% } %>', {data:lastPage});
console.log(lastPage);
})
.fail(function(){
document.location.href="/";
});
}
Result
lastPage
. If it works, I should have ‘/index’.
Issue Analytics
- State:
- Created 7 years ago
- Comments:20
Top Results From Across the Web
How to get URL parameter using jQuery or plain JavaScript?
jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use...
Read more >jQuery.get( url [, data ] [, success ] [, dataType ] )Returns: jqXHR
A set of key/value pairs that configure the Ajax request. All properties except for url are optional. A default can be set for...
Read more >4 Ways Get Query String Values from URL Parameters in ...
If you want a jQuery way to get a specific URL parameter's value then here is the function.
Read more >Variable outside ajax request - Laracasts
I want the result of ajax outside the ajax function. My code is below : var result; $.ajax({ type: "GET", url: append_url, datatype:...
Read more >Save data from AJAX request in variable (jQuery) - YouTube
jQuery : Save data from AJAX request in variable ( jQuery ) [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] jQuery ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
EJS won’t help you much with that. It will be helpful for rendering
index.ejs
.As far as passing variables from ExpressJS to navigation.js; I personally would do something like:
And then parse the JSON on the client side and do
$('#ajax-page').html(response.contents);
.Perhaps there is a better way, I never learned ExpressJS very well.
You might want to post a topic on http://stackoverflow.com for more ideas and help.
index.ejs is a full html page with
<html></html>
tags. Other ejs page are juste part of html content in order to be placed inindex.html
which is opened in the browser of the client.It’s almost like AngularJS, all ressources are gets by the client the first time he went to the site. After that, only html content is load with ajax requests. I do the same thing here.