How can the HTML code string returned by the backend be automatically rendered to the front-end page

data is submitted in the form of form form in the foreground, and the front-end page can be rendered normally through res.render (ejs) in the background. The background code is as follows:

router.post("/classifyadd", (req, res) => {
    let {classifyname} = req.body;
    Classify.create({
        name: classifyname
    }).then(doc => {
        responseData.msg = doc.name;
        
        res.render("admin/frame", {
            page: "successtip"
        });
    });
});

the problem is: when I submit data in the form of ajax, the background is written in the same way, but the front end cannot be rendered, and I see that the response header is really Content-Type: text/html; charset=utf-8 ?

I know that after the front end gets the data, it can be stuffed into the page in the form of innerHTML, but I"m not sure this is the right way.

when using AJAX request, the backend cannot directly render (res.render) and can only return data. Is there any other way to operate DOM, after the frontend gets the data?

the same question link doesn"t have a good solution yet. Thank you!

Jun.05,2021

you say that innerHTML is not the right way in the previous sentence, and you have to manipulate DOM, 's past data into the DOM tree in the second sentence.

Wu: you are not Party A, are you?

to correct you here, the form of html is actually intended to return data in the format of XML . The original ajax is for asynchronous interaction, return light data and render it yourself. You come back directly to the page. Then what else do you want ajax . It would be nice to submit the form directly.

of course, it's not that you can't do this. Just say you set up the back end and come straight here. That's fine. The plan is for innerHTML to write it in.

if you don't understand how to interact, you can refer to my own article, simple front-end interaction process (AJAX)


should only return pure data, and the front end should be rendered as HTML. Direct innerHTML can lead to XSS attacks.

Menu