简体   繁体   中英

Node.js Express: Ajax call from ejs to app.js fetching data render div dynamically

I have an express app with node.js that,

1) app.get('/', routes.index) points to my index.ejs.

2) there is a button on my index.ejs. When it is clicked, I want to call the dataGrab function in app.js to retrieve the data remotely and display in index.ejs as a <table> dynamically.

in my app.js

var data=[];
grab = function() {
   .... grab data remotely and push to data[]
}

As the button actually sits on the client side, if I want to call grab() in app.js to populate the data[] and use it to render a table on index.ejs, is using ajax the only way? If yes, could u share a simple code sample?

Regards Hammer

Thanks for tymeJV, it sends up I am using the code below ,

* edit to add ajax call

inside of my ejs I am using

<script>
$.get('/grab',function(data) {
        for(var h=0; h<data.length;h++){
    $('#newsItemTable').append('<tr><td>'+data[h].name+'</td></tr>');
        }
     });
</script>

while in my app.js I m using

app.get('/grab', function(req, res) {

....
res.send(data);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM