Ajax to refresh a partial view using express and JQuery?

后端 未结 2 1859
忘了有多久
忘了有多久 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.

    0 讨论(0)
  • 2021-01-03 03:58

    Use it You may get good solution

    function ajaxRequest(url, succesCallBck, completeCallBck, errorCallBck)<br>
    {
    $.ajax({url: url
    type: "POST",
    dataType: "json",
    complete: completeCallBck,
    success: succesCallBck,
    error: errorCallBck
    
    });
    }
    
    0 讨论(0)
提交回复
热议问题