To modify the code to return HTML instead of JSON, you can use the res.render method in Express to render an HTML template. Here's an example of how you can modify the code:

index.js:

document.getElementById("btnQuery").addEventListener("click",() => {
    const id = document.getElementById("id").value;
    const title = document.getElementById("title").value;
    const author = document.getElementById("author").value;
    const classify = document.getElementById("classify").value;

    axios({
        url: "http://localhost:3000/api/blogs/search",
        method: "get",
        params: {
            id,
            title,
            author,
            classify
        }
    }).then(results => {
        document.getElementById("results").innerHTML = results.data;
    });
});

nodejs:

exports.getSearchBlogs = (req, res) => {
    const info = req.query;
    const id = `%${info.id}%`;
    const title = `%${info.title}%`;
    const author = `%${info.author}%`;
    const classify = `%${info.classify}%`;

    const sqlStr = `select * from blogs_info where id LIKE ? or title LIKE ? or author LIKE ? or classify LIKE ?`;
    db.query(sqlStr, [id, title, author, classify], (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 0) return res.send("查询文章失败,该文章不存在");

        res.render("results", { results });
    });
};

In this example, we assume that you have a template file called "results.html" in your views folder. The res.render method will render the "results.html" file and pass the results variable to it. You can then use this variable in your HTML template to display the search results

indexjsdocumentgetElementByIdbtnQueryaddEventListenerclick = axios url httplocalhost3000apiblogssearch method get data id title author

原文地址: https://www.cveoy.top/t/topic/h2E1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录