【问题标题】:Load html from Servlet with $().load()使用 $().load() 从 Servlet 加载 html
【发布时间】:2018-12-01 20:50:50
【问题描述】:

我正在尝试从服务器加载带有 HTML 的新 jquery 选项卡:

$("#tab"+username).load("/lod1/ProfileServlet",{user:"username"},function(){
            //fill in template with user's info
            console.log("it worked");
 });

小服务程序:

    System.out.println("USERNAME HERE");
    RequestDispatcher req = request.getRequestDispatcher("/WEB-INF/profileTemplate.html");
    req.forward(request, response);

在浏览器中,我看到确认“它有效”,因此请求似乎成功了。但是在服务器上我没有看到输出“USERNAME HERE”和“#tabusername”没有填充请求的html(我可以确认它在正确的位置):

<!DOCTYPE html>

<div>
    <h1>USERS PROFILE AND THEME</h1>
</div>

【问题讨论】:

  • “/lod1/ProfileServlet”是 GET 还是 POST?提供数据后,您正在使用 POST 进行调用。
  • 它在 doGet() 中,我将 html 页面从 webinf 中移出并将 .load 的 url 更改为 '/lod1/profileTemplate.html' 删除了数据并且它有效。但是说我想通过 servlet 返回页面?为什么它不像他们在这里说的那样工作:
  • ah: "请求方法 如果数据作为对象提供,则使用 POST 方法;否则,假定为 GET。"
  • 感谢@jordiburgos

标签: javascript jquery html


【解决方案1】:

由于您的 servlet 正在侦听 GET,因此您无法将数据作为第二个数据参数传递给 .load()

使用它通过 GET 调用 servlet:

$("#tab"+username).load("/lod1/ProfileServlet?user="+username, function() {
    //fill in template with user's info
    console.log("it worked");
});

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多