question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jquery GET request and variable

See original GitHub issue

Hi!

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:closed
  • Created 7 years ago
  • Comments:20

github_iconTop GitHub Comments

1reaction
RyanZimcommented, Jun 27, 2016

Mostly

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:

router.get('/clients', function(req, res, next) {
    var response={};
    response.lastPage = '/clients';
    fs.readFile('NameOfFile', function(err, contents){
        // Error Handling
        response.content=contents;
        res.json(response);
    )};
});

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.

1reaction
LM1LC3N7commented, Jun 27, 2016

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 in index.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.

fonctionnement_site

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found