Ajax to refresh a partial view using express and JQuery?

后端 未结 2 1858
忘了有多久
忘了有多久 2021-01-03 03:13

I would like to refresh a partial view using ajax. I kwow how to append the new data to the HTML, but I would like to know if there is a simpler way to do it.

I have

2条回答
  •  一整个雨季
    2021-01-03 03:53

    First, add a route for just the partial like

    app.get('/mypartial', function (req, res) {
        //compute data here
        res.render('mypartial', {layout: false, data: data});
    });
    

    Then load the new HTML with a jQuery .get or .ajax call to /mypartial.

    Then use jQuery to replace the HTML of the parent element by doing

    $('#idofparentelementofyourpartial').html(responseHTML);
    

    See also my answer to this similar question.

提交回复
热议问题