简体   繁体   中英

Render EJS view by buttonclick

I'm working on a EJS project and i run into a problem, I want to render pages if the user click on the button.

now the jQuery looks like this:

<button onclick="show()" id="addPartial"> add more TestPartial</button>

<div id="addPartialtHere">
</div>

<script>
    function show() {
        $("#addPartialtHere").load("/test");
    }
</script> 

but it not works, due to load is only load HTML.

I also find https://github.com/tj/ejs/ , but is not updated since 2015. Thats why i think it is not the best choice.

One last thing, https://www.npmjs.com/package/express-dynamic-partials , but its not the last update was 4 years before.

Any other suggestion? Is there any way to render view in another one?

Problem with the load method in Jquery it makes ajax call to the server for the page.

And check for the Ajax response in network tab in browser.

If error is cross origin not allowed.

Try adding this code in Node server

 app.all('/', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
 });

app.get('/', function(req, res, next) {
  // Handle the get for this route
});

您可以尝试handlebarsjsmustache.js

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