简体   繁体   English

sevlet的jQuery AJAX调用在锚标记中不起作用

[英]jQuery AJAX call to sevlet not working in anchor tag

I am new to jQuery and Servlets. 我是jQuery和Servlet的新手。 Working on a school project and now I am stuck at this problem. 正在从事学校项目,现在我陷入了这个问题。

I have a navigation bar on left with anchor tags like this in my proj3.html file. 我的proj3.html文件中的左侧有一个带有锚标记的导航栏。

  <ul class="nav nav-list">

          <li class="nav-header">Labels</li>
          <li><a href="" class="labels">Sony</a></li>
          <li><a href="" class="labels">Spinnin</a></li>
          <li><a href="" class="labels">BMG</a></li>
          <li><a href="" class="labels">Ultra</a></li>
      <li><a href="" class="labels">Universal</a></li>

          <li class="nav-header">Genere</li>
          <li><a href="" class="genere">Rock</a></li>
          <li><a href="" class="genere">Alternative Rock</a></li>
          <li><a href="" class="genere">Pop</a></li>
          <li><a href="" class="genere">EDM</a></li>
          <li><a href="" class="genere">Hip-Hop</a></li>
       </ul>

Inside Javascript i have this 在Javascript里面我有这个

 $('a.labels').on('click',function(e){

        var url = "http://example.edu/servlet/SampleQuery";       
            $.get(url);
        e.preventDefault();
    }

}

Inside my servlet SampleQuery.java I read from DB and forward to a JSP like this 在我的servlet SampleQuery.java内部,我从DB读取并转发到这样的JSP

       RequestDispatcher dispatcher = null;
       dispatcher = request.getRequestDispatcher("/jsp/DisplayVendor.jsp"); 
   dispatcher.forward(request, response);

When I click on any of the anchor tags $.get is hit, but my JSP page is not getting displayed. 当我单击任意锚标记$.get ,但未显示我的JSP页面。 Weird thing is if I check the firebug for network traffic I see my AJAX call going through and I can even see DisplayVendor.jsp page in response. 奇怪的是,如果我检查萤火虫的网络流量,我会看到正在进行的AJAX调用,甚至可以看到DisplayVendor.jsp页面作为响应。 But its not getting displayed. 但是它没有被显示。

Can someone please point out if I am making any mistake. 有人可以指出我是否有任何错误。

Thanks 谢谢

You call $.get(url) , but don't do anything with the response. 您调用$.get(url) ,但不对响应做任何事情。 Change it to 更改为

$.get(url, function(data) {
    alert('here is the response data : ' + data);
});

to show what you received. 显示您收到的信息。

To update the contents of the div with id "dynamicallyLoadedDiv" with the HTML returned by the AJAX call, for example, use 例如,要使用AJAX调用返回的HTML更新ID为“ dynamicallyLoadedDiv”的div的内容,请使用

$.get(url, function(data) {
    $('#dynamicallyLoadedDiv').html(data);
});

or simply 或简单地

$('#dynamicallyLoadedDiv').load(url);

Read the documentation for more information. 阅读文档以获取更多信息。

Note that if your goal is to replace the whole page content, I don't really seee the point of making an AJAX request. 请注意,如果您的目标是替换整个页面的内容,那么提出AJAX请求的意义不大。 Use a simple hyperlink in that case. 在这种情况下,请使用简单的超链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM